.net 使用 NLog 记录异常时如何获取堆栈跟踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4684399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Stack trace when logging exceptions with NLog?
提问by Jader Dias
When I use the default layout with NLog it only prints the name of the exception. I've been told that the log4jxmlevent layout doesn't prints nothing about the exception. What layout will help me?
当我使用 NLog 的默认布局时,它只打印异常的名称。我被告知 log4jxmlevent 布局不会打印任何有关异常的信息。什么布局对我有帮助?
Example code:
示例代码:
try
{
throw new SystemException();
}
catch (Exception ex)
{
logger.Error("oi", ex);
}
Default layout output:
默认布局输出:
2011-01-14 09:14:48.0343|ERROR|ConsoleApplication.Program|oi
log4jxmlevent output:
log4jxmlevent 输出:
<log4j:event logger="ConsoleApplication.Program"
level="ERROR"
timestamp="1295003776872"
thread="9">
<log4j:message>oi</log4j:message>
<log4j:NDC />
<log4j:locationInfo class="ConsoleApplication.Program"
method="Void Main(System.String[])"
file="C:\Users\User\Documents\Visual Studio 2010\Projects\ConsoleApplication\ConsoleApplication\Program.cs"
line="21" />
<nlog:eventSequenceNumber>3</nlog:eventSequenceNumber>
<nlog:locationInfo assembly="ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<log4j:properties>
<log4j:data name="log4japp"
value="true" />
<log4j:data name="log4jmachinename"
value="MACHINE" />
</log4j:properties>
回答by Jader Dias
I had to use the one of the Logger.+ Level + Exceptionmethods:
我不得不使用Logger.+ Level +Exception方法之一:
logger.ErrorException("ex", ex);
and a custom layout
和自定义布局
layout="${exception:format=ToString,StackTrace}${newline}"
回答by Clay
Use the overloads that take an Exception as the second argument:
使用将 Exception 作为第二个参数的重载:
catch(Exception crap)
{
log.Error("Something went horribly wrong.", crap);
}
Then in your layout include the ${exception}layout renderer:
然后在您的布局中包含${exception}布局渲染器:
<target ...
layout="${longdate} ${message} ${exception:format=ToString}" />
Sources:
资料来源:
回答by Edward Brey
As documented in How to Log Exceptions, starting with NLog 4.0, pass the exception as the first parameter to Error, for example like this:
如如何记录异常中所述,从 NLog 4.0 开始,将异常作为第一个参数传递给Error,例如像这样:
logger.Error(ex, "Nickers!");
In the NLog configuration (e.g. in web.configor app.config), include ${exception:format=tostring}in the layout, for example like this:
在 NLog 配置中(例如 inweb.config或app.config),包含${exception:format=tostring}在布局中,例如像这样:
<target name="f" type="File" layout="${longdate} ${message} ${exception:format=tostring}"/>
回答by Michael Armitage
As of NLog 4.5 you can now use:
从 NLog 4.5 开始,您现在可以使用:
logger.Error(exception, message);
logger.Error(exception, message);
and layout as follows:
和布局如下:
"${longdate} ${level} ${message} ${exception:format=@}"
"${longdate} ${level} ${message} ${exception:format=@}"
The @means serialize all Exception-properties into Json-format
该@装置序列化所有异常的属性为JSON格式

