不要将日志写入数据库NLog + ASP .NET 5 + SQL Server

时间:2018-03-19 12:49:55

标签: c# asp.net nlog

我的应用程序基于ASP .NET 5构建 我曾经在.NET Core上做过一个项目,一切都很好,日志没有问题。但是在ASP.NET 5中,我不明白如何做到这一点 我尝试使用此配置将日志写入数据库:

https://github.com/nlog/NLog/wiki/Database-target#example-configurations

这是我的配置文件:

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>
  <target name="database" xsi:type="Database">

    <connectionStringName>"Server=localhost\\SQLEXPRESS;Database=LocalDB;User ID=sa;Password=1234;"</connectionStringName>


    <commandText>
      insert into dbo.LogExcelWorker (
      Application, Logged, Level, Message,
      Username,
      ServerName, Port, Url, Https,
      ServerAddress, RemoteAddress,
      Logger, CallSite, Exception
      ) values (
      @Application, @Logged, @Level, @Message,
      @Username,
      @ServerName, @Port, @Url, @Https,
      @ServerAddress, @RemoteAddress,
      @Logger, @Callsite, @Exception
      );
    </commandText>

    <parameter name="@application" layout="${appsetting:name=AppName:default=Unknown\: set AppName in appSettings}" />
    <parameter name="@logged" layout="${date}" />
    <parameter name="@level" layout="${level}" />
    <parameter name="@message" layout="${message}" />

    <parameter name="@username" layout="${identity}" />

    <parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
    <parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
    <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
    <parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />

    <parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
    <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:tostring}" />
  </target>
</targets>
  <rules>

    <logger name="*" minlevel="Debug" writeTo="database" /> 
  </rules>
</nlog>

我在数据库中创建了表

我尝试对日志进行测试调用,但没有写下任何内容:

    public string Index()
    {
        Logger log = LogManager.GetCurrentClassLogger();
        log.Trace( "trace message" );
        log.Debug( "debug message" );
        log.Info( "info message" );
        log.Warn( "warn message" );
        log.Error( "error message" );
        log.Fatal( "fatal message" );

        return "start...";
    }

但数据库中没有记录

2 个答案:

答案 0 :(得分:1)

WriteTog属性错误

<logger name="*" minlevel="Debug" writeTo="f" /> 

应该是你的情况

<logger name="*" minlevel="Debug" writeTo="database" /> 

(更改目标名称)

在这种情况下的常规启用内部日志。这对你很有帮助。这里有一个示例如何启用:

internalLogLevel="trace" 
internalLogFile="log/internal-nlog.txt"
throwExceptions="true"  
throwConfigExceptions="true"

答案 1 :(得分:0)

您需要指向正确的目标名称

<logger name="*" minlevel="Debug" writeTo="database" />