通过编程方式配置NLog

本文关键字:配置 NLog 方式 编程 | 更新日期: 2023-09-27 18:01:24

我有一个NLog的工作配置。我使用NLog.config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogFile="e:'InternalLog.txt" throwExceptions="true">
  <targets>
    <target name="database" xsi:type="Database">
      <connectionStringName>DB.Log</connectionStringName>
      <commandText>
        insert into dbo.Log (
          Logged, Level, Message, Username, Url, Logger, CallSite, Exception, Stacktrace, remoteAddress
        ) values (
          @Logged, @Level, @Message, @Username, @Url, @Logger, @CallSite, @Exception, @Stacktrace, @RemoteAddress
        );
      </commandText>
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@username" layout="${identity}" />
      <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}${aspnet-request:queryString}" />
      <parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite}" />
      <parameter name="@exception" layout="${exception:format=tostring}" />
      <parameter name="@stacktrace" layout="${stacktrace:format=DetailedFlat}" />
    </target>
  </targets>
  <rules>
    <logger name="*" writeTo="database" />
  </rules>
</nlog>

,它工作得很好。但是现在我需要以编程方式配置NLog。所以我尝试

var config = new LoggingConfiguration();
var dbTarget = new DatabaseTarget {
    ConnectionString = connectionString,
    DBProvider = "sqlserver",
    Name = "database",
    CommandText = 
        @"insert into dbo.Log (
            Logged, Level, Message, Username, Url, Logger, CallSite, Exception, Stacktrace, remoteAddress
        ) values(
            @Logged, @Level, @Message, @Username, @Url, @Logger, @CallSite, @Exception, @Stacktrace, @RemoteAddress
        );"
};
dbTarget.Parameters.Add(new DatabaseParameterInfo("@logged", "${date}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@level", "${level}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@message", "${message}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@username", "${identity}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@url", "${aspnet-request:serverVariable=HTTP_URL}${aspnet-request:queryString}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@remoteAddress", "${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@logger", "${logger}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@callSite", "${callsite}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@exception", "${exception:format=tostring}"));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@stacktrace", "${stacktrace:format=DetailedFlat}"));
config.AddTarget("database", dbTarget);
var rule = new LoggingRule("*", dbTarget);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
InternalLogger.LogFile = internalLogFile;

内部记录器工作正常,但普通记录器不行。我在配置中错过了什么?

通过编程方式配置NLog

根据我对NLog源代码的理解,如果你没有在LoggingRule的结构中提供最低日志级别,那么LogLevel.Trace将不会被用作最低日志级别。但我预料到了。实际上,似乎这是一个错误的来源:没有EnableLoggingForLevels调用(所有LoggingRule有enablelogingforlevels调用,但这)。所以,我的问题的修复只是构造函数中的一个参数:

var rule = new LoggingRule("*", LogLevel.Trace, dbTarget);
不是

var rule = new LoggingRule("*", dbTarget);

现在所有的异常将被记录,代码工作正常

需要注册NLog。Web也在c#中,这是${aspnet-request}等所需的

var extensionAssembly = Assembly.LoadFrom("nlog.web");
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(extensionAssembly);

当使用XML配置文件时,则使用自动加载(尚未在。net Core中)。

这个问题也应该在内部日志中可见。