【问题标题】:Set Event ID per log when writing to Windows Event Log写入 Windows 事件日志时为每个日志设置事件 ID
【发布时间】:2013-06-26 13:08:57
【问题描述】:

我已经像这样设置了 EventLog 目标:

<target xsi:type="EventLog"
  name="EventLog"
  layout="${longdate:universalTime=true}|${level:uppercase=true}|${logger}|${message}"
  source="MyApp"
  log="Application" />

现在,显然不是我所有的事件都具有相同的 ID,所以我想在每条消息的基础上设置事件 ID,而不是在配置中设置静态 ID。我相信这应该可行:

var logger = LogManager.GetCurrentClassLogger();

var logEvent = new LogEventInfo(LogLevel.Warn, logger.Name, "Test message");
logEvent.Properties.Add("EventID", 4444);

logger.Log(logEvent);

...但是我的事件总是将事件 ID 设置为 0。有人知道如何让这个工作吗?

【问题讨论】:

    标签: nlog


    【解决方案1】:

    我想通了 - 你必须在目标的 eventId 属性中使用布局:

    <target xsi:type="EventLog"
      name="EventLog"
      layout="${longdate:universalTime=true}|${level:uppercase=true}|${logger}|${message}"
      source="MyApp"
    >>  eventId="${event-properties:EventID:whenEmpty=0}" <<
      log="Application" />
    

    我还为 NLog 和 log4net 创建了 Timber 日志外观,这使得使用不同事件 ID 记录消息变得非常简单。

    【讨论】:

    • 我注意到如果我的日志的 eventid 为 0,它不显示,你知道为什么吗?
    【解决方案2】:

    在 github hub repo 上有一个包含 eventId 的 EventLog 目标的示例配置。 eventId 将使用呈现事件 ID 的布局。

    https://github.com/NLog/NLog/wiki/Eventlog-target

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多