You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中使用NLog无输出问题求助:自定义Target失败

NLog在Unity中无输出问题排查

我尝试查找NLog无输出问题的解决方案,但找到的内容大多是6年前或更旧的。之后我在GitHub上查看相关Issue,按照指引自定义Target但失败了。我从NuGet包获取DLL文件并添加到Unity作为插件,能调用NLog但没有任何输出。

我的nlog.config配置

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
     autoReload="true"
     throwExceptions="false"
     internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <targets>

    <target name="UnityLog" type="UnityLog" fileName="c:\log.txt" 
    layout = "[${longdate}] ${level}(${logger}): ${message} |${all-event-properties}: ${exception:format=tostring}"/>

  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="UnityLog" />
    <logger name="*" minlevel="Debug" writeTo="UnityLog" />
  </rules>

</nlog>

自定义Target代码

using NLog;
using NLog.Layouts;
using NLog.Targets;
using UnityEngine;

[Target("UnityLog")]
public class UnityLog : TargetWithLayout
{
    protected override void Write(LogEventInfo logEvent)
    {
        Name = "UnityLog";
        var msg = Layout.Render(logEvent);
        Debug.Log(msg);
    }
}

内容的提问来源于stack exchange,提问作者Observator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:55:00