|
From: Israel E. <hs....@gm...> - 2010-04-02 19:49:17
|
I have finally managed to set up a logging bridge from System.Diagnostics.Trace to NLog. In my scenario, I am specifically concerned with the Trace output from the System.Net namespace. I hope this helps anyone else who is trying to bridge Bridging System.Diagnostics.Trace to NLog. The code should fix should also allow someone to bridge to log4net. There were a couple issues I had to work around. First, the documentation on configuration is slightly wrong. Second, there is a bug in Common.Logging.Factory.AbstractLogger. For my configuration, I have the following 2 files: app.config and NLog.config My app.config looks like this: <configuration> <configSections> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog"> <arg key="configType" value="FILE" /> <arg key="configFile" value="~/NLog.config" /> </factoryAdapter> </logging> </common> <system.diagnostics> <sharedListeners> <add name="Diagnostics" type="Common.Logging.Simple.CommonLoggingTraceListener, Common.Logging" initializeData="DefaultTraceEventType=Verbose; LoggerNameFormat=System.Net.All"> <filter type="System.Diagnostics.EventTypeFilter" initializeData="Verbose"/> </add> </sharedListeners> <sources> <source name="System.Net"> <listeners> <add name="Diagnostics" /> </listeners> </source> </sources> <switches> <add name="System.Net" value="Verbose"/> </switches> <trace> <listeners> <add name="Diagnostics" /> </listeners> </trace> </system.diagnostics> </configuration> and my NLog.config (in the same directory) looks like this: <nlog> <targets> <target name="trace" xsi:type="File" fileName="${nlogdir}/System.Net.log" layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}"/> </targets> <rules> <logger name="System.Net.*" minlevel="Trace" writeTo="trace" /> </rules> </nlog> Finally, I had to make a small bug fix in Common.Logging.Factory.AbstractLogger by modifying the ToString() method. (There are constructors invocations that supply null for the IFormatProvider, but ToString() does not guard on that.) public override string ToString() { if (cachedMessage == null) { if (FormatProvider != null) { cachedMessage = string.Format(FormatProvider, Message, Args); } else { cachedMessage = Message; } } return cachedMessage; } Regards, Iz |