为 asp.net 核心 2.02 配置 Nlog

Configuring Nlog for asp.net core 2.02

我正在尝试让 NLog 在我的 asp.net 核心网络 api 2 中工作,其中 returns json 但我想要登录以跟踪使用等.

我已按照以下设置我的 nlog.config

<?xml version="1.0"?>
<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>

  <targets>
    <target name="allfile" xsi:type="File"
            fileName="${basedir}\logs\GDStationaryNetCore${shortdate}.log"
            encoding="utf-8"
            layout="[${longdate}][${machinename}][${level}] ${message} ${exception}" />
  </targets>
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

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

然后我将启动时的配置部分更改如下。

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, LoggerFactory loggerFactory)
  {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            env.ConfigureNLog("nlog.config");


    // make sure Chinese chars don't fk up
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

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

            //needed for non-NETSTANDARD platforms: configure nlog.config in your project root. NB: you need NLog.Web.AspNetCore package for this.         
            env.ConfigureNLog("nlog.config");

            app.UseMvc();
}

但是当我尝试编译时,它给出了以下内容

app.AddNLogWeb(); is obseleet use use UseNLog instead

但是当我试图在应用程序上找到它时。没有希望有人能帮忙,

AddNLogWeb 在 nlog 中已过时

参见:https://github.com/NLog/NLog.Web/blob/44f29fe12569846fa784f20a34726b82ade6526b/NLog.Web.AspNetCore/AspNetExtensions.cs#L29

使用 UseNLog:

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