Tuesday, April 17, 2012

Configure NLog ColoredConsole

I like NLog because it is probably the easiest logging framework I used. By simply copying NLog.config file to the project and set the Build Action to Content, I can use NLog in my code now. NLog support ColoredConsole. However, the default color scheme doesn't seem to make sense to  me. Here is the example, you can see the Error level is yellow. The Debug level is the same white color as Info level.

Fortunately, it's very easy to configure NLog to use different color scheme. Here is the color scheme I used.

Error/Fatal level: Red
Warn level: Yellow
Info level: White
Debug level: DarkGreen

Here is the configuration file correspondingly.


  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="console" xsi:type="ColoredConsole" layout="${longdate} [${whenEmpty:whenEmpty=${threadid}:inner=${threadname}}] ${level} ${logger} ${message} ${exception:format=tostring}">
        <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
        <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
        <highlight-row condition="level == LogLevel.Info" foregroundColor="White" />
      </target>
      <target xsi:type="File" name="file" layout="${longdate} ${level} ${logger} ${message} ${exception:format=tostring}" fileName="${basedir}/logfile.log" keepFileOpen="false" encoding="iso-8859-2" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="console" />
    </rules>
  </nlog>

2 comments:

Ben said...

I know this post is 4 years old but posting a code example as an image is not helpful.

@tonyjy said...

Thanks Ben for your comments. Just added the code example for anyone who wants to copy.

Be A Developer That Uses AI

Developers will not be replaced by AI, they'll be replaced by developers that use AI. Generative AI tools are revolutionizing the way de...