为什么NLog不使用Logger.ErrorException打印异常数据?

时间:2015-03-30 22:08:30

标签: logging nlog

我们最近实施了NLog作为我们的日志记录平台,因此我对它相对不熟悉。这一切在客户端都很好用,所以我尝试在我的WCF服务上实现它。现在我注意到Logger.ErrorException(msg,ex)只记录第一个参数,即用户消息,而不是异常本身。在调用之前我是否需要创建配置?它在文档中并不十分清楚(例如它)。

以下是我的应用程序的实际配置部分。

<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>

...这里是错误处理程序代码:

    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;
    }

异常文本包含在FaultException中并返回给客户端,但只有msg文本被记录到滚动文件中。这是我在文件中得到的内容:

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

我错过了什么?

谢谢, 戴夫

1 个答案:

答案 0 :(得分:1)

您的文件目标中似乎缺少layout=${defaultLayout}属性。

<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="File"
      name="rollingLocalFile"
      fileName="${localLogDirectory}\${machinename}-log.txt"
      archiveFileName="${localLogDirectory}\${machinename}-log.{##}.txt"
      archiveAboveSize="1048576"
      maxArchiveFiles="10"
      concurrentWrites="false"
      layout=${defaultLayout}
      keepFileOpen="true" />