NLog数据库配置无法正常工作

时间:2017-03-07 13:41:41

标签: c# asp.net asp.net-mvc config nlog

我一直在尝试链接中给出的所有方法,但仍然无法弄清楚为什么我的日志没有保存在数据库中。我正在使用SQL Server 2012。

这是我正在进行的配置。

<?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"
  autoReload="true"
  internalLogLevel="Warn"
  internalLogFile="c:\temp\internal-nlog.txt">

 <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
 </extensions>

  <!-- define various log targets -->
 <targets>
<!-- write logs to file -->
<target xsi:type="Null" name="blackhole" />

<target name="database" xsi:type="Database" >

  <connectionString>
    Data Source=DESKTOP-APAPHS0\SQLEXPRESS;Initial Catalog=NLog;Integrated Security=True;
  </connectionString>

  <commandText>
    insert into dbo.log (
    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="Logger" />
  <parameter name="@logged" layout="${date}" />
  <parameter name="@level" layout="${level}" />
  <parameter name="@message" layout="${message}" />

  <parameter name="@username" layout="${aspnet-user-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-url:IncludeHost=boolean:IncludePort=boolean:IncludeQueryString=boolean}" />
  <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>
  <!--All logs, including from Microsoft-->
   <logger name="*" minlevel="Trace" writeTo="database"></logger>

      <!--Skip Microsoft logs and so log only own logs-->
      <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole"   final="true" />
</rules>
</nlog>

我在startup.cs文件中也包含了nlog以及Configure方法。

//add NLog to ASP.NET Core
        loggerFactory.AddNLog();

        //add NLog.Web
        app.AddNLogWeb();

但它还没有登录数据库。我在这里想念的是什么我正在使用这些版本。

"NLog.Extensions.Logging": "1.0.0-rtm-beta1",
"NLog.Web.AspNetCore": "4.3.0",

0 个答案:

没有答案