【问题标题】:The right way to log to EventLog using NLog使用 NLog 记录到 EventLog 的正确方法
【发布时间】:2013-08-13 01:08:53
【问题描述】:

在使用 NLog 进行日志记录时,如何避免 Windows 抱怨缺少事件 ID 的描述。当我使用时:

<target xsi:type="EventLog" 
        name="eventLog" 
        layout="${message}" 
        machineName="."     
        source="MyApp" 
        log="Application" />

<rules>
    <logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>

该条目将出现在日志中。但是 Windows 抱怨缺少对事件 ID“0”的描述,这是正确的。

我是否必须执行pointed out here 之类的操作才能获得干净的日志记录?

【问题讨论】:

  • 你见过这样的问题吗:stackoverflow.com/questions/17320933/…?
  • @nemesv 是的,但是我在哪里设置 ID?
  • 这取决于您想在 ID 中包含什么,您可以使用链接问题中描述的一些自定义值,也可以使用任何内置渲染器:github.com/nlog/NLog/wiki/Layout-Renderers
  • @nemesv 工作但仍然丑陋。但这不是 NLog 的错,所以到目前为止我的问题已经得到解答。随意添加答案,我会标记它。

标签: .net logging configuration nlog


【解决方案1】:

我知道这是一个旧帖子,但配置应该是

<target xsi:type="EventLog" 
        name="eventLog" 
        layout="${message}"
        machineName="."     
        source="MyApp"
        log="Application"
        eventId="${event-properties:EventID:whenEmpty=0}" />

<rules>
    <logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>

另请参阅:https://github.com/NLog/NLog/wiki/EventLog-target

【讨论】:

  • 谢谢。这不是问题。我刚刚更正了我上面的问题。这都是关于记录到 Windows EventLog 的。如何告诉 NLog 使用某个事件 ID。动态 ID 会更好。
  • @AlexanderSchmidt 您可以为目标指定eventId-property,并在记录时提供EventId。
【解决方案2】:

根据 NLog 文档,可以设置一个 eventId 标签。 https://github.com/nlog/NLog/wiki/EventLog-target

<targets>
  <target xsi:type="EventLog"
          name="String"
          layout="Layout"
          machineName="String"
          source="Layout" 
          category="Layout"
          eventId="Layout"
          log="String" />
<!-- note: source is a string in NLog before 4.0 -->

</targets>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2018-11-02
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多