log4net traceappender only logs messages with level 'verbose' when using Windows Azure DiagnosticsMonitor

后端 未结 1 412
说谎
说谎 2021-02-02 16:42

I have an azure worker role which I have configured to use a log4net Trace Appender which writes to WindowsAzure.Diagnostics. This is done by making the following calls in the R

相关标签:
1条回答
  • 2021-02-02 17:23

    A couple of blog posts deal with this issue: (here and here)

    • It turns out that the log4net TraceAppender converts all log messages to Trace.Write messages and that the DiagnosticMonitorTraceListener converts all Trace.Write messages to verbose.

    The answer in my case was to use Pete McEvoy's solution and extend the TraceAppender in the following manner:

    using System.Diagnostics;
    using log4net.Appender;
    using log4net.Core;
    namespace XXX.Logging
    {
        public class AzureTraceAppender : TraceAppender
        {
            protected override void Append(LoggingEvent loggingEvent)
            {
                var level = loggingEvent.Level;
                var message = RenderLoggingEvent(loggingEvent);
    
                if (level >= Level.Error)
                  Trace.TraceError(message);
                else if (level >= Level.Warn)
                  Trace.TraceWarning(message);
                else if (level >= Level.Info)
                  Trace.TraceInformation(message);
                else
                  Trace.WriteLine(message);
                if (ImmediateFlush)
                  Trace.Flush();
            }
        }
    }
    

    This extension was then implemented in my App.config:

    <log4net>
        <appender name="AzureTraceAppender" type="XXX.Logging.AzureTraceAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%logger - %message" />
            </layout>
        </appender>
        <root>
            <level value="ALL" />
            <appender-ref ref="AzureTraceAppender" />
        </root>
    </log4net>
    
    0 讨论(0)
提交回复
热议问题