【发布时间】:2019-03-14 11:16:57
【问题描述】:
我正在运行 Windows 服务, 我想当我在 debugto 中运行时写入控制台 以及何时作为事件查看器的服务。
在powershell中我设置了
New-EventLog –LogName Application –Source "mySource"
我有这个nlog.config:
<nlog>
<targets>
<target name="debugger" type="Debugger" layout="${logger}::${message}"/>
<target name="console" type="Console" layout="${logger}::${message}"/>
<target name="file" type="File" layout="${longdate} ${logger}::${message}" fileName="${basedir}/Logs/${shortdate}.log"/>
<target name="eventLog" type="eventlog" layout="${logger}::${message}" source="mySource"/>
</targets>
<rules>
<logger name="" minlevel="Trace" writeTo="debugger"/>
<logger name="" minlevel="Trace" writeTo="console"/>
<logger name="*" minlevel="Trace" writeTo="file"/>
<logger name="*" minlevel="Debug" writeTo="eventLog" />
</rules>
</nlog>
我在服务启动时进行初始化:
public static void InitLogger()
{
NLog.Targets.Target target = null;
target = LogManager.Configuration.FindTargetByName("eventlog");
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Info);
LogManager.Configuration.Reload();
}
为了测试这一点,我在两种情况下都将其更改为写入“事件日志” 即使在调试模式下。但使用事件查看器时无法正常工作(VS 以管理员模式运行)
我在每个班级都设置了
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
缺少什么?
【问题讨论】:
-
更新:我发现偶数观众在这里写字(见图)pasteboard.co/I5ngzT8.png
-
很高兴也能看到你的
nlog.config -
看不到任何名为
eventlog的目标 -
@RolfKristensen 我更新了之前的答案
-
你应该只有一个 NLog 配置。不在 app.config 和 nlog.config 中。
标签: c# logging nlog event-viewer