Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically log System.diagnostics.trace messages to an Nlog target

Say you have C# trace messages all over an application. Something like:

Trace.TraceInformation("Service Started"); 

How do you automatically log this to an nLog target without having to add code like the following to all the classes that have trace messages?

using NLog;
private static Logger logger = LogManager.GetCurrentClassLogger();

Is there a way to do this without including traces produced by the .NET Framework itself, which this article demonstrates how to do?

like image 425
Solracnapod Avatar asked Nov 06 '12 22:11

Solracnapod


1 Answers

This works for cases where there isn't an explicit source.

  <system.diagnostics>
      <trace autoflush="true" indentsize="4">
        <listeners>
          <add name="MyNLogTraceListener" type="NLog.NLogTraceListener, NLog" />
          <remove name="Default" />
        </listeners>
      </trace>
  </system.diagnostics>
like image 170
fiat Avatar answered Oct 12 '22 01:10

fiat