在生产模式下,删除ASP.NET Core 2.0中的控制台和调试记录器

时间:2017-08-31 16:47:30

标签: c# asp.net-core asp.net-core-2.0

在ASP.NET Core 2.0中我们有这个

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

CreateDefaultBuilder(args)有许多有用的默认值。但是contains this

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

因此,控制台和调试日志记录提供程序始终已注册。

我曾经像这样注册它们

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

在生产模式下运行时如何删除/取消注册? 我不是指改变日志记录级别,我的意思是我不希望他们在生产模式下注册。

3 个答案:

答案 0 :(得分:30)

我会说设计的方法是通过更改日志记录配置而不是将任何内容记录到这些提供程序。但我知道您要删除任何生产要求;你仍然可以在代码中正确地做到这一点。

您可以从传递给HostBuilderContext lambda的ConfigureLogging访问托管环境:

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

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

显然,仅凭这一点无法撤消CreateDefaultBuilder已设置的呼叫。首先,您需要取消注册这些提供程序。为此,您可以使用新的this logging issue on GitHub方法:

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

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

这是为回应Parallax Effect而引入的。

答案 1 :(得分:3)

我认为您无法使用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"
  }
}

},

答案 2 :(得分:1)

我发现最好从服务中删除特定的日志记录提供者,如下所示:

.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();
})
相关问题