我有NLog包的Logger, 我想在我的配置记录器文件中定义 trace enable=true。
我该怎么做?
我的 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" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" maxlevel="Trace" minlevel="Trace" writeTo="logfile" />
</rules>
</nlog>
最佳答案
您可以通过在 NLog.config 的 rules
部分设置 minLevel
属性来做到这一点;
<rules>
<logger name="*" minlevel="Debug" writeTo="CSVFile" />
</rules>
Debug
会写出Debug
、Info
、Warn
、Error
和致命
级别的日志消息。仅跳过 Trace
。
编辑
相反,如果您只想记录Trace
,则像这样设置maxLevel
;
<rules>
<logger name="*" maxlevel="Trace" writeTo="CSVFile" />
</rules>
要仅记录特定的中间级别,请同时使用 minLevel
和 maxLevel
;
<rules>
<logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="CSVFile" />
</rules>
EDIT2
我调整了您的配置文件,使输出记录到控制台而不是文件;
<?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" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" maxlevel="Trace" writeTo="logconsole" />
</rules>
</nlog>
当我在测试应用程序中运行时,我会像这样写出多个日志记录级别;
_logger.Trace("trace text");
_logger.Debug("debug text");
_logger.Info("info text");
_logger.Warn("warn text");
_logger.Error("error text");
_logger.Fatal("fatal text");
并且只有 Trace 文本显示在控制台窗口中。但是,需要注意的一件事是配置文件中的 XML 必须正确格式化。上面的示例按照我在此处的测试应用程序中的描述工作。
关于c# - 如何在 nlog.config 中设置 debugEnabled/InfoEnabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49028413/