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>

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...