Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog Colored Console

I am using NLog to log errors. Here is the Config code

<target name="console" xsi:type="AsyncWrapper" >
      <target  xsi:type="ColoredConsole"  layout="${longdate:padding=-10}${callsite:className=false:includeSourcePath=false:methodName=false} | ${message}" >
         <highlight-row condition="level >= LogLevel.Info" foregroundColor="Green" backgroundColor="NoChange"/> 
      </target>
    </target>

I have a custom property set on the log event like

private LogEventInfo GetLogEvent(string loggerName, LogLevel level, string message, ConsoleColor color)
        {
    var logEvent = new LogEventInfo(level, loggerName, message);

                logEvent.Properties["color"] = color;// color= any console color
}

and this sets the "color" property.(lets say "Red" here)

and I am trying to use this "color" property in the target like

 <highlight-row condition="equals('${color}','Red')" foregroundColor="Red" backgroundColor="NoChange"/> 

this dosent work and I tried

<highlight-row condition="equals('${event-context:item=color}','Red')" foregroundColor="Red" backgroundColor="NoChange"/> 

but no luck.

Am I missing something or is there a better way of doing this? Can we use Layout renderers in this case? If yes how do we implement this?

like image 818
Gokul Avatar asked Nov 03 '22 12:11

Gokul


1 Answers

First, since you are storing values in LogEventInfo.Properties, you should be using your second configuration example, which gets the value from event-context.

I have not used the ColoredConsoleTarget, so take this as a suggestion, not as something that I know for a fact will work.

I suspect that the NLog Condition object does not know about the ConsoleOutputColor enum. So, when you store a ConsoleOutputColor enum value in LogEventInfo.Properties, the Condition does not know that 'Red' (in the condition) refers to ConsoleOutputColor.Red. I have two suggestions:

First option: store the string value of the ConsoleOutputColor in LogEventInfo.Properties. Using ToColor might be sufficient. Something like this:

var logEvent = new LogEventInfo(level, loggerName, message);
logEvent.Properties["color"] = color.ToString();

Then, in your Condition, you should be able to compare against the ConsoleOutputColor string values (what you have in your configuration might be right, if you store the color name string as I suggested).

If that doesn't work, you could try...

Second option: store the ConsoleOutputColor value in LogEventInfo.Properties, as you are doing now, but change your condition in the config file to compare the "color" from event-context to the numeric value of the ConsoleOutputColor values. Something like this (I have not tried this so I don't know for sure that it is correct):

<highlight-row condition="equals('${event-context:item=color}','12')" foregroundColor="Red" backgroundColor="NoChange"/>

(In the ConsoleOutputColor enum, Red is 12).

like image 110
wageoghe Avatar answered Nov 09 '22 14:11

wageoghe