Nlog不会写入Windows服务中的日志文件

问题描述 投票:4回答:1

我有一个用.net 4.0编写的Windows服务,我把它安装在Local System的凭证下。在我使用nlog的代码中,

private static Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("some information");

我也将nlog.config复制到exe文件所在的同一目录

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="logfile" type="File"
            fileName="c:\log\TestService\My.log"
            layout="${longdate}::${logger}::${message}"
            keepFileOpen="false" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" maxLevel="Deubg" writeTo="logfile" />
  </rules>
</nlog>

但是,如果我启动该服务,我根本看不到日志文件。如果我将log.Debug代码交换到Debug.WriteLine并使用我的Visual Studio附加到Windows服务进程进行调试,我可以看到输出窗口有我的调试消息,这意味着我的Windows服务代码是正确的。

我的nlog代码有问题吗?

- 更新1 -

我将nlog.config更新为

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="logfile" type="File"
            fileName="c:\log\TestService\My.log"
            layout="${longdate}::${logger}::${message}"
            keepFileOpen="false" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile" />
  </rules>
</nlog>

并将我的代码更改为

logger.Trace("some information");

然后它工作。我不确定我的第一组配置有什么问题。

c# windows-services nlog
1个回答
10
投票

UPDATE

<logger name="*" minlevel="Info" maxLevel="Deubg" writeTo="logfile" />
                                           ^^^^^

首先:Debug拼写错误。

但主要问题是Debug级别在信息级别下。

以下是允许的日志级别(按降序排列):

  • 致命
  • 错误
  • 警告
  • 信息
  • 调试
  • trace(最详细的信息。)

Link to the nlog documentation

© www.soinside.com 2019 - 2024. All rights reserved.