为什么 NLog 不使用 Logger.ErrorException 打印异常数据?
Why is NLog not printing exception data with Logger.ErrorException?
我们最近实现了 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
我错过了什么?
谢谢,
戴夫
您的文件目标中似乎缺少 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" />
我们最近实现了 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
我错过了什么?
谢谢, 戴夫
您的文件目标中似乎缺少 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" />