I would like to do some info logging with log4net on console and use multiple colors in each line. This is my current simplified configuration:
...
<appender name="ConsoleDebug" type="log4net.Appender.ColoredConsoleAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
</filter>
<mapping>
<level value="INFO" />
<foreColor value="Green, HighIntensity" />
</mapping>
...
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger{1} %message%newline" />
</layout>
</appender>
...
This outputs all my logging messages as green text. Is it possible to output %logger
as white text and %message
as green?
I thought for a moment that ANSI escape codes could have worked but apparently it is not possible anymore in latest OSes. Any other solution I see would have you do some non trivial amount of work.
To handle switching colors in the layout of your message you could do the following:
That's a lot of code to write to get custom colors "done right" (ie your layout is independent from the appender it's working with).
There is a way to cheat a bit, if you consider that only a Console appender uses your layout:
Append
method.Append
method split along markers that tell you what color you want and process them chunk by chunk to get the color swaps you want.This could be a quick and dirty solution but the layout will be only usable with this appender. If you need to switch appenders you would have to override it again. Here is an example of such an appender:
public class ConsoleAppenderWithColorSwitching : ConsoleAppender
{
// forfeits the auto switch from Console.Error to Console.Out
// of the original appender :/
protected override void Append(LoggingEvent loggingEvent)
{
var regex = new Regex(@"(\|\w+\|)");
var renderedLayout = base.RenderLoggingEvent(loggingEvent);
var chunks = regex.Split(renderedLayout);
foreach (var chunk in chunks)
{
if (chunk.StartsWith("|") && chunk.EndsWith("|"))
{
var consoleColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), chunk.Substring(1, chunk.Length - 2));
Console.ForegroundColor = consoleColor;
}
else
{
Console.Write(chunk);
}
}
}
}
This lets you change the foreground color in the layout with the following syntax: |Green| %logger{1} |Red|%message%newline
. This is really a proof of concept but feel free to experiment with it...
All in all I wouldn't recommend you go down this path. It is definitely possible but it may be more work than you are comfortable investing in dynamic color switches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With