-1

i try to find the answer but they are almost 6 years ago or older.then i see the issue on github. i try to custom a target following github but failed

i get the dll file from nuget package and add to unity as a plugin, then i can use nlog but without any output.

below is my 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>

and the definition of 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);
    }
}

1 Answer 1

1

You are specifying fileName="c:\log.txt" but your custom UnityLog-target doesn't have a FileName-property.

If you want output to both UnityEngine.Debug and also file-path, then you should setup 2 NLog-targets:

<?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"
      throwConfigExceptions="true"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  <targets>

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

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

  </targets>

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

</nlog>

Notice that the C-Root-Drive is usually off-limits, so changed from C:\Log.txt to C:\Temp\Log.txt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.