Search code examples
log4netsystem.diagnostics

Log4net traceappender not logging anything


I'd like to redirect everything logged by log4net to the System.Diagnostics Trace classes. My understanding of what I should be doing is pointing log4net at system.diagnostics.traceappender, then I configure system.diagnostics. Here's important parts in my web.config:

<log4net>
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
  <immediateFlush value="true" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern"
         value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
  <priority value="DEBUG"/>
  <appender-ref ref="trace"/>
</root>    
</log4net>


<system.diagnostics>
<sources>

  <source name="Console" switchName="DefaultSwitch">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
    </listeners>
  </source>

  <source name="Metabase" switchName="MetabaseSwitch">
    <listeners>
      <add name="MetabaseListener" />
      <remove name="Default" />
    </listeners>
  </source>

  <source name="TextFile" switchName="TextFileSwitch">
    <listeners>
      <add name="TextFileListener" />
      <remove name="Default" />
    </listeners>
  </source>

</sources>
<sharedListeners>
  <!--<add name="ConsoleListener" type="XXX.Manufacturing.Utilities.Diagnostics.ColorConsoleTraceListener,XXX.Manufacturing.Utilities" />-->
  <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
  <add name="MetabaseListener" type="XXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener,XXX.Metabase.Proxies" />
</sharedListeners>
<switches>
  <add name="MetabaseSwitch" value="Information" />
  <add name="DefaultSwitch" value="Verbose" />
  <add name="TextFileSwitch" value="Verbose"/>
</switches>
</system.diagnostics>

Did I miss a crucial step linking things up? If I bypass log4net and just create a new tracesource it will log to my sources.


Solution

  • I added the call to XmlConfigurator.Configure() and turned on internal logging. What I saw was log4net was logging, but nothing was reaching the trace system. After playing with my app.config for a while I found a configuration that worked, most notable changes seemed to be ditching the sources in my Systems.Diagnostics config and making sure the log4net level attribute was set. Working config sections:

    <log4net>
      <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
        <immediateFlush value="true" />
        <layout type="log4net.Layout.PatternLayout,log4net">
          <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
        </layout>
      </appender>
    
      <root>
        <level value="ALL"/>
        <appender-ref ref="trace"/>
      </root>    
    </log4net>
    
    <system.diagnostics>
      <trace autoflush="true" >
        <listeners>
          <add name="TextFileListener" />
          <add name="MetabaseListener" />
        </listeners>
      </trace>
      <sharedListeners>
        <add name ="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextFile.log" />
        <add name="MetabaseListener" type="XXXX.Manufacturing.Utilities.Diagnostics.MetabaseTraceListener, XXXX.Metabase.Proxies" />
      </sharedListeners>
    </system.diagnostics>