以编程方式配置NLog

时间:2016-08-08 16:29:14

标签: c# logging configuration .net-4.5 nlog

我正在配置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;

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

2 个答案:

答案 0 :(得分:2)

至于我理解NLog的源代码,如果你没有在name的结构中提供最小日志级别,LoggingRule将不会被用作最小日志级别。但我预计它会。实际上,似乎它是源代码中的错误:没有LogLevel.Trace调用(all LoggingRule have EnableLoggingForLevels call but this)。所以,我的问题的解决方案只是构造函数中的一个参数:

EnableLoggingForLevels

而不是

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

现在将记录所有异常并且代码正常工作。

答案 1 :(得分:0)

您还需要在C#中注册NLog.Web,这需要var extensionAssembly = Assembly.LoadFrom("nlog.web"); ConfigurationItemFactory.Default.RegisterItemsFromAssembly(extensionAssembly); 等。

STYLE


    COLOR 160,160,100
       END STYLE #
       STYLE
         OUTLINECOLOR 100 100 100
         WIDTH 1
       END STYLE #

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

此问题也应在内部日志中可见。