繁体   English   中英

为ASP.NET Core 2.02配置Nlog

[英]Configuring Nlog for asp.net core 2.02

我正在尝试使NLog在我的asp.net核心Web API 2中工作,该API返回json,但我想获取loggin以进行跟踪使用等。

我已经按照以下步骤设置了我的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(); 过时了,请改用UseNLog

但是,当我尝试在应用程序上找到它时。 那里不是希望有人可以帮助,

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM