如何在AspNetCore 2.2 NLog中关闭Microsoft日志

时间:2019-05-08 01:15:01

标签: asp.net-core .net-core nlog

以下是我的日志

  

[2019-05-07 15:55:38.4270] [PC-20170829ROEW] [Info]我的日志记录了

     

[2019-05-07 15:55:38.4929] [PC-20170829ROEW] [Info]路由与{action =>“ Get”,controller =“ Values”}匹配。执行动作> QunarFlight.Web.Controllers.ValuesController.Get(QunarFlight.Web)

     

[2019-05-07 15:55:38.5798] [PC-20170829ROEW] [Info]执行操作方法> QunarFlight.Web.Controllers.ValuesController.Get(QunarFlight.Web)-验证>状态:有效

     

[2019-05-07 15:55:38.6066] [PC-20170829ROEW] [Info]执行的操作方法> QunarFlight.Web.Controllers.ValuesController.Get(QunarFlight.Web),返回>结果Microsoft.AspNetCore.Mvc .ObjectResult在22.6363毫秒内。

     

[2019-05-07 15:55:38.6066] [PC-20170829ROEW] [Info]执行ObjectResult,>写入类型为'System.String []'的值。

我想关闭所有Microsoft默认日志(最后四条记录),仅保留我已记录的日志(第一条记录)。 我在以下内容上修改了appsettings.json

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore.Hosting": "Warning",
    "Microsoft.AspNetCore.Routing": "Warning",
    "Microsoft.AspNetCore.Mvc": "Warning"
 }

在这种情况下,将不会输出我记录的日志。如果我删除“ Microsoft.AspNetCore.Mvc”:“警告”,我的记录日志将被输出。如何修改?

appsettings.json

"Logging": {
   "LogLevel": {
     "Default": "Information",
     "Microsoft.AspNetCore.Hosting": "Warning",
     "Microsoft.AspNetCore.Routing": "Warning",
     "Microsoft.AspNetCore.Mvc": "Warning"
}

在Startup.cs中配置

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        env.ConfigureNLog("NLog.config");
        loggerFactory.AddNLog();
        app.UseMiddleware<CustomMiddleware>();
        app.UseMvc();
    }

Program.cs

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((builderContext, config) =>
        {
            config.AddJsonFile("appsetting.json", optional: true, reloadOnChange: true)
             .AddJsonFile($"appsettings.{builderContext.HostingEnvironment.EnvironmentName}.json", true, true)
             ;
        })
            .UseStartup<Startup>();

1 个答案:

答案 0 :(得分:1)

如果您在NLog.config的文件布局中包含了${logger},那么帮助您设置记录器筛选器会容易得多。

但是我在Google上发现了这个随机线索:

Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Information: Executing ObjectResult

所以我想您需要将MEL-Config更改为此:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore.Hosting": "Warning",
    "Microsoft.AspNetCore.Infrastructure": "Warning",
    "Microsoft.AspNetCore.Routing": "Warning",
    "Microsoft.AspNetCore.Mvc": "Warning"
 }

甚至可以考虑这样做:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft.AspNetCore": "Warning"
 }