【发布时间】:2020-02-27 08:26:51
【问题描述】:
我在 C#.NET 中有一个带有最新版本的 .NET 框架的 windows-service 项目,我正在尝试在项目中实现 nLog 4.5.11。 Windows 服务有一个计时器类,它的事件每 10 秒触发一次。我正在尝试从这些处理程序写入 nLog,但它没有在日志文件中打印。
windows onstart 事件处理程序的 info 语句正在写入日志,非常好。
在 internalLogFile.log 中进行故障排除后,现在我找到了 nLog Info 的那些行,但在另一个由其他程序集(如 RabbitMQ.log)创建的日志文件中。我什至没有在 nLog.config 中提到它。我不确定为什么 nLog 会在多个文件中混合内容。
nlog 程序集添加到 windows-service 项目:-
nLog.config 文件内容:-
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="C:\Px\log.txt" internalLogLevel="Info">
<targets>
<target name="logconsole" xsi:type="Console" />
<target name="logfile"
xsi:type="File"
fileName="${basedir}/Px_LoaderService-${shortdate}.log" />
</targets>
<rules>
<logger name="docLogger" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="logconsole" />
</rules>
</nlog>
InternalLogfile 也没有任何错误或有关丢失写入的信息。
这里是 定时器事件处理程序的示例代码:-
private static NLog.Logger log = LogManager.GetLogger("docLogger");
private void BulkInserts(object sender, ElapsedEventArgs e)
{
lock (_locker)
{
try
{
log.Info(string.Format("BulkInsert called."));
}
}
请建议我在哪里犯了错误以及这种混合的日志写入正在发生。
【问题讨论】:
-
您是否已经证明除了 reasonable-doubt 计时器实际上正在触发?是的,你可以attach-to和break-point一个windows服务
-
定时器事件处理程序运行良好。我可以看到每 10 秒触发一次断点。
-
这应该可以在线程(和计时器事件处理程序)中正常工作,但我们看不到的谜题肯定还有更多。
-
我还需要在 nLog 中配置什么吗?请提出建议。
-
internalLogFile 是什么意思?也许将 internalLogLevel 设置为 Trace。还要检查github.com/nlog/nlog/wiki/Logging-troubleshooting
标签: c# windows-services nlog