I am using log4net DebugAppender (or TraceAppender). I have configured the appender like this:
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %message%newline" />
</layout>
</appender>
Loggers in the code are declared in the usual per-class manner:
private static readonly ILog Log =
LogManager.GetLogger(typeof(TradingApiRouteCollectionExtensions));
Output in the Output windows looks like this:
Acme.Common.Configuration.TradingApiRouteCollectionExtensions: DEBUG Registering route prefix 'session' for service Acme.Session.SessionService Acme.Common.Configuration.TradingApiRouteCollectionExtensions: DEBUG Web methods found for type Acme.Session.SessionService: Acme.Common.Configuration.TradingApiRouteCollectionExtensions: DEBUG session/
Notice how every line starts with the logger type name. I want to suppress this as I didn't ask for it in the configuration and I don't want it. I can't see any obvious way to do this. Is it possible?
You need to create your own appender. The one you are using does the following:
System.Diagnostics.Debug.Write(RenderLoggingEvent(loggingEvent), loggingEvent.LoggerName);
if (!this.m_immediateFlush)
return;
System.Diagnostics.Debug.Flush();
Therefore you always end up with the class (logger) name in the output window. You can derive from the log4net Debug appender and override the Append
method.
Alternatively, you can start your layout conversion pattern with %newline
. From my config:
<appender name="DebuggerAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%file (%line): %level: %message%newline"/>
</layout>
</appender>
Pros: it's quicker than creating your own appender
Cons: it shows you log on 2 lines.
Note how the %file (%line)
at the beginning of the line is a format interpreted by Visual Studio. It allows you to click on the log message in the output window and be taken straight to the the code that generated it.
If you read the TraceAppender source code, you'll find actually the logger name is wrote as the default "category" name of the trace. So if you set this value already, the logger name will not be displayed in the output.
So with the config below:
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<category value="" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline%exception" />
</layout>
</appender>
The output will be:
: 2017-02-27 22:53:26,335 [6] INFO - Task Ended
But you may find that every line starts with ":", which is ugly. So my config for category is:
...
<category value="LOG" />
...
And the output is:
LOG: 2017-02-27 22:53:26,335 [6] INFO - Task Ended
NOTE: DebugAppender does not provide the way to overwrite category. Only TraceAppender works.
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