Nlog: How to output NLog log to Rider's console

Created on 13 Sep 2020  Â·  4Comments  Â·  Source: NLog/NLog

I use Rider as the editor of Unity. How do I redirect the log output from Nlog to Rider's console?

external nlog-configuration question

Most helpful comment

Rider seems to have support for displaying Unity Console:

https://blog.jetbrains.com/dotnet/2018/04/10/view-unity-console-logs-directly-rider-2018-1/

You can write to Unity Console using UnityEngine.Debug:

You can make you own NLog-target like this:

```c#
[Target("UnityDebugLog")]
public class UnityDebugLogTarget : TargetWithLayout
{
protected override void Write(LogEventInfo logEvent)
{
string logMessage = RenderLogEvent(this.Layout, logEvent);
if (logEvent.LogLevel <= LogLevel.Info)
UnityEngine.Debug.Log(message);
else if (logEvent.LogLevel == LogLevel.Warn)
UnityEngine.Debug.LogWarning(message);
else
UnityEngine.Debug.LogError(message);
}
}


And you can [configure the target](https://github.com/nlog/nlog/wiki/Tutorial) together with Unity-Bootstrap:

```c#
using System.IO;
using UnityEngine;

public static class Bootstrap
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void ConfigureLogging()
    {
       var config = new NLog.Config.LoggingConfiguration();

       // Targets where to log to:
       var unityConsole = new UnityDebugLogTarget() { Name = "Unity" };

       // Rules for mapping loggers to targets            
       config.AddRule(LogLevel.Info, LogLevel.Fatal, unityConsole);

       // Apply config           
       NLog.LogManager.Configuration = config;

       // Generate output
       NLog.LogManager.GetCurrentClassLogger().Info("Hello World");
    }
}

All 4 comments

Rider seems to have support for displaying Unity Console:

https://blog.jetbrains.com/dotnet/2018/04/10/view-unity-console-logs-directly-rider-2018-1/

You can write to Unity Console using UnityEngine.Debug:

You can make you own NLog-target like this:

```c#
[Target("UnityDebugLog")]
public class UnityDebugLogTarget : TargetWithLayout
{
protected override void Write(LogEventInfo logEvent)
{
string logMessage = RenderLogEvent(this.Layout, logEvent);
if (logEvent.LogLevel <= LogLevel.Info)
UnityEngine.Debug.Log(message);
else if (logEvent.LogLevel == LogLevel.Warn)
UnityEngine.Debug.LogWarning(message);
else
UnityEngine.Debug.LogError(message);
}
}


And you can [configure the target](https://github.com/nlog/nlog/wiki/Tutorial) together with Unity-Bootstrap:

```c#
using System.IO;
using UnityEngine;

public static class Bootstrap
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void ConfigureLogging()
    {
       var config = new NLog.Config.LoggingConfiguration();

       // Targets where to log to:
       var unityConsole = new UnityDebugLogTarget() { Name = "Unity" };

       // Rules for mapping loggers to targets            
       config.AddRule(LogLevel.Info, LogLevel.Fatal, unityConsole);

       // Apply config           
       NLog.LogManager.Configuration = config;

       // Generate output
       NLog.LogManager.GetCurrentClassLogger().Info("Hello World");
    }
}

@snakefoot
Thank you very much. Your plan is complete and effective. The log can indeed be output to the Rider console, but I encountered a problem. If I switch the unity scene, the rider will automatically clear the console log. I don’t want him to clear mine log, is there any solution for this?

@ZhouJiaZhi If I switch the unity scene, the rider will automatically clear the console log. I don’t want him to clear mine log, is there any solution for this?

I have never used Rider or Unity3D. This place is primary for NLog-specific issues/questions. Maybe try the support-forum for Rider / Unity3d or try StackOverflow

@snakefoot
OK, Thank you

Was this page helpful?
0 / 5 - 0 ratings