views:

36

answers:

2

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.

A: 

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>
Stefan Egli
Added a call to XmlConfigurator.Configure, still not logging. I will try enabling internal debugging.
Justin Holbrook
A: 

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>

Justin Holbrook