NLog.Web.AspNetCore不再记录到数据库

时间:2018-12-26 20:36:41

标签: c# nlog

我有一个dotnet核心API,该版本在带有NLog.Web.AspNetCore 4.7.0(NLog 4.5.11)的2.2版本上可用,该版本以前在4.5.4版本中可以使用。使用此更新,现在看来似乎没有要登录到数据库。在我的代码中,关于日志记录基础结构的任何内容都没有改变。

我应该提到,我的日志中也有这条可爱的线,所以我知道它有些用。但是我通常会获得更多信息,当然,数据库中也会写入一些信息。

setActiveIndex($select) {
    $timeout(() => {
        if($select.selected === undefined) {
            $select.activeIndex = -1;
        }
    }, 25);
}

我尝试在github问题中及此处查找,但是我发现没有什么用处,非常感谢您的帮助!

Program.cs中的初始设置

2018-12-26 15:06:59.9503||DEBUG|SomeApp.API.Program|init main |url: |action: 

实施

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
    logger.Debug("init main");
}
catch (Exception ex)
{
    //NLog: catch setup errors
    logger.Error(ex, "Stopped program because of exception");
    throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
    NLog.LogManager.Shutdown();
}

nlog配置

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseIISIntegration()
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                })
                .UseNLog()  // NLog: setup NLog for Dependency injection
                .Build();

示例记录代码

<?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="Trace"
  internalLogFile="C:\temp\internal-nlog.txt">

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

 <targets async="true">

 <target type="File" name="file" fileName="${basedir}\logs\logfile.txt"
  maxArchiveFiles="5" archiveAboveSize="20971520" archiveEvery="Day"
  layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

<target xsi:type="Null" name="blackhole" />

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


  <connectionString>${gdc:item=connectionString}</connectionString>

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

  <parameter name="@EventId" layout="${event-properties:item=EventId_Id}" />
  <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: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>
<!--Limit to only configured logs for all logs, change name="*"-->
<logger name="SomeApp.*" minlevel="Info" writeTo="database" />
<logger name="*" minlevel="Trace" writeTo="file" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
</rules>
</nlog>

appsettings.json和appsettings.Development.json日志记录设置

_logger.LogInformation(1001, "Login success: {0}", userForLoginDto.Username);

NLog内部日志似乎没有任何意义,但是我可以根据需要提供它。

对于任何奇怪的格式化问题表示歉意,nlog.config文件不想随便出现。

0 个答案:

没有答案