Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop the Visual Studio Output window displaying the logger name of log4net output?

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?

like image 517
James World Avatar asked Feb 07 '12 10:02

James World


3 Answers

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.

like image 144
Stefan Egli Avatar answered Nov 07 '22 11:11

Stefan Egli


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.

like image 10
elmotec Avatar answered Nov 07 '22 12:11

elmotec


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.

like image 4
Mason Zhang Avatar answered Nov 07 '22 10:11

Mason Zhang