Log4net traceappender not logging anything

Justin Holbrook picture Justin Holbrook · Sep 2, 2010 · Viewed 11.4k times · Source

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.

Answer

Stefan Egli picture Stefan Egli · Sep 3, 2010

Did you call the XmlConfigurator.Configure(); method?

If that is not the problem then you can either turn on internal debugging (explained here) or maybe configure a console appender and see if that is working.

Edit: I am not that familiar with the trace system, but if you configure a trace listener as follows you should get the log4net output:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add
         name="textWriterTraceListener"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="C:\temp\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>