【问题标题】:NLog change eventlog target to something else than ApplicationNLog 将 eventlog 目标更改为 Application 以外的其他内容
【发布时间】:2019-05-21 13:17:33
【问题描述】:

我将目标日志从应用程序更改为“xxxx”。 我使用 powersheel 删除了源 并在“xxxx”下创建它。 当我写事件时,它不会出现在任何地方。

target = LogManager.Configuration.FindTargetByName("eventlog");
mode = config.RUN_MODE;
LogManager.Configuration.RemoveTarget("console");
((NLog.Targets.EventLogTarget)target).Log = "xxxx";
LoggingConfiguration loggingConfiguration = new LoggingConfiguration();
var asyncFileTarget = new AsyncTargetWrapper(target);
loggingConfiguration.AddTarget("eventlog", asyncFileTarget);
LogManager.Configuration = loggingConfiguration;
LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Fatal, target, mode, true);
LogManager.ReconfigExistingLoggers();

我使用 powershell 删除并添加了新日志和源(“xxxx”日志已创建):

Remove-EventLog -Source "mySource" 
New-EventLog -LogName xxxx -Source "mySource code here

【问题讨论】:

  • PS:你可以使用target = LogManager.Configuration.FindTargetByName<NLog.Targets.EventLogTarget>("eventlog");——这样可以节省演员表

标签: c# nlog event-viewer


【解决方案1】:

创建新事件日志时,服务器需要重新启动。还要确保该帐户对事件日志具有写入权限。

同样重要的是,将 Configuration 属性分配给 LogManager 会激活配置,因此建议先添加规则。

所以不是:

LogManager.Configuration = loggingConfiguration;
LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Fatal, target, mode, true);

但是:

loggingConfiguration.AddRule(LogLevel.Info, LogLevel.Fatal, target, mode, true);
LogManager.Configuration = loggingConfiguration; // activates config

如果还有问题,建议关注NLog Troubleshooting guide

【讨论】:

  • 我尝试了所有,我什至更改了 app.config 中的 log="xxxx" 但它仍然没有写入新的事件日志
猜你喜欢
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2014-07-05
  • 2013-09-01
  • 1970-01-01
  • 2017-12-12
  • 2021-09-13
  • 2010-12-01
相关资源
最近更新 更多