使用NLog记录异常时如何获取堆栈跟踪? [英] How to get the Stack trace when logging exceptions with NLog?

查看:1465
本文介绍了使用NLog记录异常时如何获取堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用NLog的默认布局时,它只会打印出异常的名称。
我被告知,log4jxmlevent布局不会打印任何关于异常。
什么布局将帮助我?

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?

示例代码:

try
{
    throw new SystemException();
}
catch (Exception ex)
{
    logger.Error("oi", ex);
}

默认布局输出:

2011-01-14 09:14:48.0343|ERROR|ConsoleApplication.Program|oi

log4jxmlevent输出:

log4jxmlevent output:

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

推荐答案

我不得不使用 Logger之一。 + Level + 异常方法:

I had to use the one of the Logger. + Level + Exception methods:

logger.ErrorException("ex", ex);

和自定义布局

layout="${exception:format=ToString,StackTrace}${newline}"

这篇关于使用NLog记录异常时如何获取堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆