【问题标题】:how to change the source value of the NLog eventlog target如何更改 NLog 事件日志目标的源值
【发布时间】:2021-09-27 20:21:41
【问题描述】:

是否有可能动态更改 NLog.config 的源名称,如果可以,请任何人帮助我。

<targets>
<target xsi:type="EventLog" name="eventlog" layout="${callsite}:${message}" log="Application" **source="MyAwesomeSolution"**/>

这是我想用来自其他方法的参数更改为 NLog.Config 文件的源名称。 source="MyAwesomeSolution"

提前致谢。

【问题讨论】:

  • 是否有可能在运行时动态更改源@WahidBitar

标签: c# .net logging nlog


【解决方案1】:

您可以在源字段中使用任何预定义变量或自定义字段。

例如source="${event-properties:item=customName}"

在你的代码中,你可以填写这个值

logger.Info("this event is coming from may {customName}", "my app");

或者你可以使用

logger.SetProperty("customName", "my app");

甚至

logger.WithProperty("customName", "my app").Info("test message");

更多详情请查看文档

https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer#example

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

【讨论】:

  • 不工作...实际上什么都没有发生
  • 我猜你没有按照我分享的第二个链接中的说明创建源
  • 安装/卸载时,仅当源不包含布局渲染器时才创建/删除事件源。安装时,事件源只能在以管理员身份运行时创建。或者,创建此源的简单 PowerShell 命令如下所示(以管理员身份运行 PowerShell)。 New-EventLog -LogName Application -Source "MySourceName"
  • 消息来了,但问题是源名称不会根据所选实例@Wahid Bitar而改变
  • 这看起来是正确的:)(赞成)。还有其他设置上下文的方法,例如GDC。在此处查看所有选项:github.com/NLog/NLog/wiki/Context
猜你喜欢
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多