Roman
Roman

Reputation: 161

Extra logs from API

Does anyone know how to filter (remove) unnecessary logs from API function calls? They pollute logs) .NET Core. I was trying to confige logging via appsettings.json, but it didn't work. I need to avoid them at all, or better to reduce logs in the Trace/Info/ modes. Im using NLog in Program\StartUp, Microsoft Extension Logging in other..

Trace: sample trace logs

UPDATE

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>

Program:

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

logs for API creates automatically.

enter image description here

<logger name="Microsoft.AspNetCore" minLevel="None"* - does not effecr on anything(.

Update. Thanks to Qiang Fu. Right nlog.config is:

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

Upvotes: 0

Views: 96

Answers (1)

Qiang Fu
Qiang Fu

Reputation: 8781

The appsettings affect on defualt logger not Nlog. You need to modify the Nlog.config like following:

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

The minLevel and maxLevel for "Microsoft.*" should include all levels. (Trace:0 ,Fatal:6)

If you want configuration "write to Console only >= Error (4) level", you could configure like following : (Also you need configure all levels of "Microsoft.*". If not, the left levels will use the "*" settings)

    <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>

Upvotes: 2

Related Questions