配置NLog在XML输出中记录异常? [英] Configuring NLog to log exceptions in an XML output?

查看:71
本文介绍了配置NLog在XML输出中记录异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们有NLog吐出CSV文件只是为了证明我们有NLog实际上记录了异常.

Currently, we have NLog spitting out CSV files just to prove we have NLog actually logging exceptions.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogToConsole="true" internalLogToConsoleError="true">
<targets>
  <target name="file" xsi:type="File"  fileName="${specialfolder:folder=ApplicationData}/log.csv">
    <layout xsi:type="CSVLayout">
      <column name="User_Machine_Name" layout="${machinename}" />
      <column name="Time" layout="${date}" />
      <column name="Level" layout="${level}" />
      <column name="Message" layout="${message}" />
      <column name="Exception_Message" layout="${exception:format=Message}"/>
      <column name="Exception_Type" layout="${exception:format=Type}"/>
      <column name="Callsite_Class" layout="${callsite:methodName=false}" />
      <column name="Callsite_Method" layout="${callsite:className=false}" />
      <column name="Stack_Trace" layout="${stacktrace:format=DetailedFlat}"/>
    </layout>
  </target>
  <target name="console" xsi:type="Console"
    layout="${longdate}|${level}|${message}">
  </target>
</targets>
<rules>
  <logger name="*" minlevel="Trace" writeTo="file" />
</rules>

这按预期工作,除了我需要它以XML输出.我浏览了NLog文档,发现的唯一发现是有一个Log4JXmlEventLayout,但是文档没有涉及如何使用它.我是NLog的新手,我在该主题上找不到太多资源.

This is working as expected except that I need it to output in XML. I've looked through NLog documentation and the only thing I've found is that there's a Log4JXmlEventLayout but the documentation doesn't go into how to use it. I'm new to NLog and I can't find too many resources on the subject.

推荐答案

据我所知,Log4JXmlEventLayout具有一些与之相关的属性(堆栈跟踪信息,调用类,时间等),但这仅仅是关于它.我研究了如何包括其他信息,但这似乎是不可能的.

As far as I can tell, the Log4JXmlEventLayout has some properties associated with it (stack trace-ish information, calling class, time, etc), but that's about it. I've looked into how to include additional information, but it seems as if that's not possible.

可能的配置如下:

<target name ="xmlFile" xsi:type="File"
                fileName="${tempdir}/${processname}/log.xml"
                archiveFileName="${tempdir}/${processname}/archive/log_{#####}.xml"
                archiveAboveSize="10000000"
                layout="${log4jxmlevent:includeSourceInfo=true:includeCallSite=true:includeMDC=true:appInfo=true:includeNDC=true:includeNLogData=true}"/>

但是,我发现只有NLog 2.0会真正使用"includeSourceInfo"之类的属性.在我看来,在NLog 1.0中,将它们设置为true时,生成的xml仅包含日期,级别和消息.

However, I've found that only NLog 2.0 will actually make use of attributes like "includeSourceInfo". It appeared to me that in NLog 1.0, when these were set to true, the resulting xml contained only the date, level, and message.

不使用Log4JXmlEventLayout的一个原因是它对异常没有任何作用,例如,如果您调用

One reason not to use Log4JXmlEventLayout is that it doesn't do anything with exceptions, i.e. if you call

logger.ErrorException("This shouldn't happen", exception);

记录器将写下这不应该发生",但异常信息消失了.

the logger will write down "This shouldn't happen", but the exception info is gone.

也许可以围绕所需数据创建自定义xml包装器.我还没有这样做,但我只是因为Log4JXmlEventLayout的局限性才考虑这一点.

Maybe it would be possible to create a custom xml wrapper around the data you want. I haven't done so, but I'm thinking about it simply due to the limitations around the Log4JXmlEventLayout.

这篇关于配置NLog在XML输出中记录异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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