NLog Trace Listener for System Diagnostics Trace - NLog/NLog GitHub Wiki
Captures output from System Diagnostics Trace and redirects to NLog Targets.
- DisableFlush - Ignore flush operations from System.Diagnostics.Trace (Default = True)
- DefaultLogLevel - Use this default LogLevel for all LogEvents without any LogLevel.
- ForceLogLevel - Always use this LogLevel for all LogEvents (independent on the one provided).
- AutoLoggerName - Always capture StackTrace for each LogEvent and resolve Logger-name from callsite (Default = False)
Update app.config
to redirect from the WCF ServiceModel-source to NLog Target output:
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="All">
<listeners>
<add name="nlog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="nlog" type="NLog.NLogTraceListener, NLog" />
</sharedListeners>
</system.diagnostics>
and
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
/>
</diagnostics>
</system.serviceModel>
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.Listeners.Add(new NLog.NLogTraceListener { Name = "NLog" });
System.Diagnostics.Trace.WriteLine("Hello World");
NLogTraceListener is threadsafe and supports multithreading. To improve concurrency then ensure useGlobalLock is false (Default = true). See also LowLevelDesign.org - Case of deadlock
NLogTraceListener does not need help with flushing, since this is handled automatically by the NLog engine. To improve performance then ensure autoflush is false (Default = false).
<system.diagnostics>
<trace useGlobalLock="false" />
<trace autoflush="false" />
</system.diagnostics>
If having setup NLogTraceListener
and at the same time using custom NLog Target that depends on System.Diagnostics.Trace
for diagnostics tracing, then one can experience StackOverflowException
. Since it is just like calling NLog Logger from NLog Target, which is also forbidden.
Possible solution is to setup filtering of the particular Trace-source, so it doesn't become a recursive loop.