|
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 |
|
From: Erich E. <eei...@gm...> - 2010-04-07 08:19:54
|
Hi Israel, thanks a lot for your input, I will fix that as soon as possible -Erich > -----Original Message----- > From: Israel Evans [mailto:hs....@gm...] > Sent: Friday, April 02, 2010 8:49 PM > To: net...@li... > Subject: [Netcommon-developer] Bridging System.Diagnostics.Trace to > NLog (bug fix) > > 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 > > ----------------------------------------------------------------------- > ------- > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev > _______________________________________________ > Netcommon-developer mailing list > Net...@li... > https://lists.sourceforge.net/lists/listinfo/netcommon-developer |