Shawn de Wet
Shawn de Wet

Reputation: 5996

NLogTraceListener how to output traceOutputOptions

I am using the NLogTraceListener which I found here.

It is defined in my config file as follows:

<sharedListeners>
      <add name="nlog" type="NLog.NLogTraceListener, NLog" traceOutputOptions="Callstack" />
</sharedListeners>

And here is the LayoutRenderer in the nlog.config file:

layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring}"

And here is the sample output of an error to the log file:

2018-08-07 06:37:46.3350 System.Net.Sockets ERROR 200 [16956] Exception in Socket#8977203::Connect - An operation was attempted on something that is not a socket 23.96.28.38:443.

How do I get my layout to include the traceOutputOptions (in this case, the callStack)

Upvotes: 0

Views: 850

Answers (2)

Rolf Kristensen
Rolf Kristensen

Reputation: 19877

There is no support for traceOutputOptions in NLogTraceListener.

Instead you just configure the layout to include the ${callsite}.

Example:

layout="${longdate} ${logger} ${uppercase:${level}} ${threadid} ${message} ${exception:format=tostring} ${callsite}"

See the Wiki for how to configure output options: https://github.com/NLog/NLog/wiki/Callsite-layout-renderer

Upvotes: 1

Julian
Julian

Reputation: 36800

Update:

Misread your question at first.

The stacktrace should be logged if possible, see

https://github.com/NLog/NLog/blob/f65cc2ace0ebbe60cc5839b2a0f5e9417d04797e/src/NLog/NLogTraceListener.cs#L443-L446

If that isn't helping, then you could be saved with a custom TraceListener.

  1. Inherit from NLogTraceListener.
  2. Override ProcessLogEventInfo . See also original code of NLogTraceListener

e.g. (C#)

 public class MyNLogTraceListener : NLogTraceListener
 {
     protected override void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int? eventId, TraceEventType? eventType, Guid? relatedActiviyId)
     {
         //copy of NLog
         loggerName = (loggerName ?? Name) ?? string.Empty;

         StackTrace stackTrace = null;
         int userFrameIndex = -1;
         if (AutoLoggerName)
         {
             stackTrace = new StackTrace();
             for (int i = 0; i < stackTrace.FrameCount; ++i)
             {
                 var frame = stackTrace.GetFrame(i);
                 loggerName = Internal.StackTraceUsageUtils.LookupClassNameFromStackFrame(frame);
                 if (!string.IsNullOrEmpty(loggerName))
                 {
                     userFrameIndex = i;
                     break;
                 }
             }
         }

         ILogger logger;
         if (LogFactory != null)
         {
             logger = LogFactory.GetLogger(loggerName);
         }
         else
         {
             logger = LogManager.GetLogger(loggerName);
         }

         logLevel = ForceLogLevel ?? logLevel;
         if (!logger.IsEnabled(logLevel))
         {
             return; // We are done
         }

         var ev = new LogEventInfo();
         ev.LoggerName = loggerName;
         ev.Level = logLevel;
         if (eventType.HasValue)
         {
             ev.Properties.Add("EventType", eventType.Value);
         }

         if (relatedActiviyId.HasValue)
         {
             ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
         }

         ev.Message = message;
         ev.Parameters = arguments;
         ev.Level = ForceLogLevel ?? logLevel;

         if (eventId.HasValue)
         {
             ev.Properties.Add("EventID", eventId.Value);
         }

         if (stackTrace != null && userFrameIndex >= 0)
         {
             ev.SetStackTrace(stackTrace, userFrameIndex);
         }

         //something here with base.TraceOutputOptions <-------------------- 
         /


         logger.Log(ev);



     }
 }

Usage:

<sharedListeners>
      <add name="nlog" type="MyNameSpace.MyNLogTraceListener, MyDllName" traceOutputOptions="Callstack" />
</sharedListeners>

Upvotes: 0

Related Questions