代码之家  ›  专栏  ›  技术社区  ›  Corey Ogburn

配置nlog以在XML输出中记录异常?

  •  9
  • Corey Ogburn  · 技术社区  · 14 年前

    目前,我们有nlog输出csv文件来证明我们有nlog实际记录异常。

    <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,在这个问题上找不到太多资源。

    1 回复  |  直到 14 年前
        1
  •  10
  •   llaughlin    14 年前

    据我所知,log4jxmleventlayout有一些与之相关联的属性(堆栈跟踪ISH信息、调用类、时间等),但就是这样。我已经研究过如何包含附加信息,但似乎不可能。

    可能的配置如下:

    <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只包含日期、级别和消息。

    不使用log4jxmleventlayout的一个原因是它不做任何异常的事情,即如果调用

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

    日志记录器将写下“这不应该发生”,但异常信息已不存在。

    也许可以围绕您想要的数据创建一个定制的XML包装器。我还没有这样做,但是我只是考虑到log4jxmleventlayout的局限性。