Delete console and debug logs in ASP.NET Core 2.0 in production mode

In ASP.NET Core 2.0 we have this

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();

This CreateDefaultBuilder(args)has many useful defaults. But contains this :

.ConfigureLogging((context, logging) => {
    logging.AddConfiguration(context.Configuration.GetSection("Logging"));
    logging.AddConsole();   // HERE IS THE PROBLEM
    logging.AddDebug();     // HERE IS THE PROBLEM
})

Thus, console and debug logging providers are always registered.

I used them to register

if (env.IsDevelopment())
{ 
    // register them here
}

How to remove / unregister at startup in production mode? I do not mean a change in the level of logging, I mean that I do not want them to be logged at all in working mode.

+23
source share
4 answers

, - , . , ; .

HostBuilderContext, ConfigureLogging lambda:

.ConfigureLogging((context, logging) =>
{
    logging.AddConfiguration(context.Configuration.GetSection("Logging"));

    if (context.HostingEnvironment.IsDevelopment())
    {
        logging.AddConsole();
        logging.AddDebug();
    }
});

, , CreateDefaultBuilder. -, . ILoggingBuilder.ClearProviders:

.ConfigureLogging((context, logging) =>
{
    // clear all previously registered providers
    logging.ClearProviders();

    // now register everything you *really* want
    // …
});

GitHub.

+34

, CreateDefaultBuilder LogLevels None. .

public static void Main(string[] args)
{
var webHost = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var env = hostingContext.HostingEnvironment;
        config.AddJsonFile("appsettings.json", optional: true, 
reloadOnChange: true)
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", 
optional: true, reloadOnChange: true);
        config.AddEnvironmentVariables();
    })
    .ConfigureLogging((hostingContext, logging) =>
    {


logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
    })
    .UseStartup<Startup>()
    .Build();

webHost.Run();
}

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x

, appsettings.json

  "Logging": {
"IncludeScopes": false,
"LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
},
"Console": {
  "LogLevel": {
    "Default": "None"
  }
}

},

+3
I have not even tried it myself, just a thought. Declare a static property public static IHostingEnvironment Environment { get; set; } and add logging based on this support. Unverified code again :) use it at your own risk
  public static IHostingEnvironment Environment { get; set; }

  public Startup(IHostingEnvironment env)
  {
           Environment = env;
  }

   public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseIISIntegration()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseKestrel()
            .UseStartup<Startup>()
            .ConfigureLogging(factory =>
            {
                if (Environment.IsDevelopment())
                {
                    factory.AddConsole();
                    factory.AddDebug();
                }
            })
            .Build();
}
0
source

I found that it is better to remove a specific logging provider from services as follows:

.ConfigureLogging((context, logging) => {
    foreach (ServiceDescriptor serviceDescriptor in logging.Services)
    {
        if (serviceDescriptor.ImplementationType == typeof(Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider))
        {
            // remove ConsoleLoggerProvider service only
            logging.Services.Remove(serviceDescriptor);
            break;
        }
    }

    // now you can register any new logging provider service; e.g.,
    logging.AddLog4Net();
    logging.AddEventSourceLogger();
})
0
source

Source: https://habr.com/ru/post/1687381/


All Articles