DaveN59
DaveN59

Reputation: 3728

Why is NLog not printing exception data with Logger.ErrorException?

We recently implemented NLog as our logging platform so I am relatively unfamiliar with it. It all works great on the client so I tried to implement it on my WCF service. Now I notice that Logger.ErrorException(msg, ex) is logging only the first parameter, the user message, and not the exception itself. Is there configuration I have to create prior to calling this? It's not very clear in the documentation (such as it is).

Here is the actual config section for my application.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="localLogDirectory"
          value="C:\ProdMonLogs" />
<!-- there is a typo on this line. There should be additional '}'s at the end of the format string to close the onexception block, 
     but it appears there is an error in NLog's parser that causes it to get printed out instead of interpreted as a part of the format string. -->
<variable name="defaultLayout"
          value="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:inner=${newline}${exception:format=StackTrace :innerFormat=ShortType, Message, StackTrace  :maxInnerExceptionLevel=10 :separator=${newline}${newline}------ :innerExceptionSeparator=${newline}${newline}*----------*" />

<targets async="true">
  <target xsi:type="File"
          name="rollingLocalFile"
          fileName="${localLogDirectory}\${machinename}-log.txt"
          archiveFileName="${localLogDirectory}\${machinename}-log.{##}.txt"
          archiveAboveSize="1048576"
          maxArchiveFiles="10"
          concurrentWrites="false"
          keepFileOpen="true" />
  <target xsi:type="ColoredConsole"
          name="console"
          layout="${defaultLayout}"
          errorStream="false" />
  <target xsi:type="Chainsaw"
          name="localViewer"
          onOverflow="Split"
          layout="${defaultLayout}"
          address="udp://127.0.0.1:9999" />
</targets>

<rules>
  <logger name="*"
          minLevel="Warn"
          writeTo="console, rollingLocalFile"/>
</rules>
</nlog>

...and here is the error handler code:

    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

    /// <summary>
    /// Handles most generic exceptions thrown by service methods by wrapping the exception details in 
    /// a generic FaultException. This causes a FaultException to be thrown on the client side while
    /// not faulting the channel, so the client can unpack the real exception for handling.
    /// </summary>
    /// <param name="ex">The exception.</param>
    internal static void HandleException(Exception ex)
    {
        var msg = "There was an error processing the request" + Environment.NewLine;

        Logger.ErrorException(msg, ex);

        if (OperationContext.Current != null)
        {
            throw new FaultException(msg + ex);
        }

        throw ex;
    }

The exception text gets wrapped in the FaultException and returned to the client but only the msg text is logged to rolling file. Here's what I get in the file:

2015-03-30 17:50:44.5862|ERROR|Services.Util|There was an error processing the request

What am I missing?

Thanks, Dave

Upvotes: 3

Views: 2463

Answers (1)

arid1
arid1

Reputation: 116

Looks like you're missing a layout=${defaultLayout} attribute in your File target.

<target xsi:type="File"
      name="rollingLocalFile"
      fileName="${localLogDirectory}\${machinename}-log.txt"
      archiveFileName="${localLogDirectory}\${machinename}-log.{##}.txt"
      archiveAboveSize="1048576"
      maxArchiveFiles="10"
      concurrentWrites="false"
      keepFileOpen="true" />

should be

<target xsi:type="File"
      name="rollingLocalFile"
      fileName="${localLogDirectory}\${machinename}-log.txt"
      archiveFileName="${localLogDirectory}\${machinename}-log.{##}.txt"
      archiveAboveSize="1048576"
      maxArchiveFiles="10"
      concurrentWrites="false"
      layout=${defaultLayout}
      keepFileOpen="true" />

Upvotes: 2

Related Questions