Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog console highlight properties

Tags:

c#

nlog

I've watched the following video explaining about Serilog. I would like to use structured logging in my application as well (at least parts of it), but I prefer to use NLog (1) because it's already part of our stack and we're used to it and prefer to have the same logger on across our projects, and (2) I have read the comparison here and seems like NLog is more performant.

So I read that NLog also support structured logging and I have implemented it in a test application without a problem. It works very well.

What I saw in the video and liked about Serilog, is when writing to console it highlights the parameters passed to the logging function like this:

Example of Serilog console parsing

I would like to have the same on the console application I'm currently building. I have tried both Console target and ColoredConsole but non has that effect. Is it possible in NLog?

This is my targets configurations:

<target name="file"
            xsi:type="File"
            archiveEvery="Day"
            archiveFileName="Logs\log.{#}.txt"
            fileName="Logs\log.txt"
            archiveNumbering="DateAndSequence"
            archiveDateFormat="yyyy-MM-dd"
            archiveAboveSize="104857600"
            maxArchiveFiles="30"
            layout="${longdate} | ${uppercase:${level}} | ${logger} | ${threadid} | ${message} ${exception}"
            />

    <target xsi:type="ColoredConsole"
          name="ColorConsole"
          layout="${uppercase:${level}}: ${message} ${exception:innerFormat=Message,StackTrace}"
          header="Memoriez API"
          useDefaultRowHighlightingRules="false"
          >
      <highlight-word foregroundColor="Green" ignoreCase="true" text="info" wholeWords="true" />
      <highlight-word foregroundColor="Red" ignoreCase="true" text="warn" wholeWords="true" />
      <highlight-word backgroundColor="Red" foregroundColor="White" ignoreCase="true" text="error" wholeWords="true" />
      <highlight-row backgroundColor="DarkRed" foregroundColor="Yellow" condition="level == LogLevel.Fatal" />
    </target>
like image 292
developer82 Avatar asked Oct 18 '25 17:10

developer82


1 Answers

I do not think you can colorized the parameters in ColoredConsoleTarget. It might be possible to use WordHighlighting, but you would quickly run into issues where matching a number would highlight all numbers and not just the parameter.

My guess is that you would need to write a custom ColoredConsoleTarget in order to highlight parameters. I just looked at the src\NLog\Targets\ColoredConsoleTarget.cs file and it only offers RowHighlightingRules and WordHighlightingRules. It looks like when the color codes are applied the LogEventInfo has been rendered into a plain string.

I think you would need to write a custom RenderLogEvent function that would render the color escape sequences for parameters. It would be a bit tricky because the GenerateColorEscapeSequences for WordHighlighting would escape any color sequences generated before it is called).

Here are my thoughts:

  1. Create a new class ColoredParamConsoleTarget with code copied from ColoredConsoleTarget. It doesn't look like there are virtual methods to just override the existing class.
  2. Create a ColoredRenderLogEvent method and parse for parameters and add color sequences before and after parameters.
  3. Replace calls to RenderLogEvent with ColoredRenderLogEvent
like image 60
Ryan Avatar answered Oct 20 '25 07:10

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!