While writing a windows service, I created a console version for debugging use. I wanted this console to display all
the logging, in real time, so I could see things were working. A bit of a search revealed that ColoredConsoleAppender
was the desired class, so I added code to set things up.
var layout
= new PatternLayout("%level [%thread] %d{HH:mm:ss} - %message%newline");
mConsoleLogger
= new ColoredConsoleAppender
{
Name = "ConsoleAppender",
Layout = layout,
Threshold = Level.All
};
var repository = (Hierarchy)LogManager.GetRepository();
repository.Root.AddAppender(mConsoleLogger);
repository.Root.Level = Level.All;
But, it didn’t work. Not at all. Back to the search engines.
Eventually, I found the (odd) solution.
Before adding the appender into the log4net repository, you need to activate it:
mConsoleLogger.ActivateOptions();
Once this is done, everything started working perfectly.
Not the way I’d write it - requiring odd incantations or other ceremony on the part of a class consumer seems fairly unfriendly.