来自 API 的额外日志

问题描述 投票:0回答:1

有谁知道如何过滤(删除)API函数调用中不必要的日志?它们污染日志).NET Core。我试图通过 appsettings.json 配置日志记录,但它不起作用。我需要完全避免它们,或者更好地减少跟踪/信息/模式中的日志。 我在 Program\StartUp 中使用 NLog,在其他中使用 Microsoft 扩展日志记录..

追踪:

更新

appsettings.json:

  "Logging": {
    "LogLevel": { 
      "Default": "Error",
      "Microsoft": "Error",
      "Microsoft.Hosting.Lifetime": "Error",
      "Microsoft.AspNetCore": "Error" //<----i hoped it will help
    },
    "EventLog": {
      "LogLevel": {
        "Default": "None",
        "Microsoft": "None",
        "Microsoft.Hosting.Lifetime": "None"
      }
    }
  },

nlog.config:

  <rules>
    <logger name="*" minLevel="Info" writeTo="file, kafka, console" />
    <logger name="Microsoft.AspNetCore*" minLevel="None" final="true" />
  </rules>
</nlog>

节目:

public static void Main(string[] args)
{
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProjectConstants.FOLDER_CONFIGS);
    var logger = NLogBuilder.ConfigureNLog(Path.Combine(path, "nlog.config")).GetCurrentClassLogger();
    logger.Info("init main");
    var builder = new ConfigurationBuilder()
        .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
        .AddJsonFile(Path.Combine(path, "appsettings.json"), true);
    Configuration = builder.Build();
    CreateHostBuilder(args).Build().Run();
}
    public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =>
          {
              webBuilder.UseKestrel()
                  .UseConfiguration(Configuration)
                  .UseStaticWebAssets()
                  .UseStartup<Startup>()
                  /*
                  .ConfigureLogging(logging =>
                  {
                      logging.ClearProviders();
                      logging.SetMinimumLevel(LogLevel.Trace);
                  })
                  */
                  .UseNLog();
          }).UseWindowsService().UseSystemd();
}

API 日志自动创建。

应用程序设置影响默认记录器而不是 Nlog。您需要修改 Nlog.config,如下所示:

    <rules>
        <logger name="Microsoft.*" minLevel="Trace" maxLevel="Fatal" final="true" />
        <logger name="*" minLevel="Info" writeTo="file, kafka, console" />  
    </rules>

“Microsoft.*”的 minLevel 和 maxLevel 应包括所有级别。 (痕迹:0,致命:6)

如果您想配置“仅写入控制台> =错误(4)级别”,您可以配置如下:(同时您需要配置“Microsoft.*”的所有级别。如果没有,左侧级别将使用“*” “设置)

    <rules>
        <logger name="Microsoft.*" minLevel="Trace" maxLevel="Warning" final="true" />
        <logger name="Microsoft.*" minLevel="Error" maxLevel="Fatal" writeTo="console" final="true" />
        <logger name="*" minLevel="Info" writeTo="file, kafka, console" />  
    </rules>
c# .net asp.net-core logging
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.