在 NLog 中选择自定义日志级别

Selecting a custom log level in NLog

我正在为应用程序编写 NLog 文件。所需的要求之一是用户应该能够 select 文件大小和日志级别。可以从下拉菜单中 select 编辑日志级别。我在想我可以有一个文件大小和日志级别的占位符。我该怎么做呢?目前,我已经这样做了:

<target name="logfile" xsi:type="File" FileName=".\logs${shortdate}.log" 
        archiveFileName=".\logs\archive${shortdate}.log"
        maxArchiveFiles="60" archiveEvery="Day" archiveAboveSize="###MaxSize###"
        layout="${longdate}|${level:uppercase=true}|${callsite}|${message}|${exception:format=toString}"/>
    <rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />

我有两个问题:

  1. 我怎样才能让日志级别可以select选择?
  2. 是否需要为每个日志级别设置不同的target

建立在Combine nlog.config and config from code

之上

Please note that combining the config file (nlog.config) and changing it in code, the reload of nlog.config could undo your changes. If you combine both, then reapply the changes on the reload event. E.g.

// On start of your program
UpdateConfig();

LogManager.ConfigurationReloaded += (sender, e) =>
{
    //Re apply if config reloaded
    UpdateConfig();
};

Where UpdateConfig is

public void UpdateConfig()
{
    var configuration = LogManager.Configuration;
    var fileTarget = configuration.FindTargetByName<FileTarget>("myTargetName");
    fileTarget.FileName = "${basedir}/file.log";
    LogManager.Configuration = configuration; //apply
}

然后,我将其调整为

public void UpdateConfig()
{
    var configuration = LogManager.Configuration;

    // Get User-Defined LogLevel and FileSize from App Settings
    // Set NLog LogLevel and FileSize

    LogManager.Configuration = configuration; //apply
}

在 Log-Config-Dialog 的处理程序中,我将 Level 和 FileSize 写入 App Settings,然后调用 UpdateConfig

确保为这些应用设置提供默认设置或处理尚未设置的设置。

半动态路由规则

NLog 4.6.7 可以轻松地即时更改 LogLevel:

<nlog>
   <variable name="myLevel" value="Trace" />
    <rules>
      <logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
    </rules>
</nlog>

然后像这样从运行时更改它:

LogManager.Configuration.Variables["myLevel"] = "Debug";
LogManager.ReconfigExistingLoggers();

https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

重写并重新加载NLog.config

但听起来您应该只在 NLog.config 文件中执行搜索和替换,然后执行显式重新加载:

<nlog>
    <variable name="myLevel" value="Trace" />
    <variable name="mySize" value="42" />
    <targets>
        <target name="logfile" xsi:type="File"
              fileName=".\logs${shortdate}.log" 
              archiveAboveSize="${mySize}" />
    </targets>
    <rules>
      <logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
    </rules>
</nlog>

在 file-rewrite/update(改变两个变量)之后,显式重载是这样完成的:

NLog.LogManager.Configuration = NLog.LogManager.Configuration?.Reload();

显式重新加载的替代方法是使用 <nlog autoReload="true">(然后 NLog 将检测对 NLog.config 文件的更改并自动重新加载)。

重写并重新加载NLog.user.config

如果你感到兴奋并且喜欢高级的东西,那么你也可以使用包含文件,并且有一个默认的 NLog.config 包括 NLog.user.config:

<nlog autoreload="true">
    <variable name="myLevel" value="Trace" />
    <variable name="mySize" value="42" />
    <include file="NLog.user.config" ignoreErrors="true" />  <!-- Can override variables -->
    <targets>
        <target name="logfile" xsi:type="File"
              fileName=".\logs${shortdate}.log" 
              archiveAboveSize="${mySize}" />
    </targets>
    <rules>
      <logger name="*" minlevel="${var:myLevel}" writeTo="logfile" />
    </rules>
</nlog>

另请参阅:https://github.com/NLog/NLog/wiki/XML-config-include-Example