zoukankan      html  css  js  c++  java
  • ASP.NET Core 禁止命令行打印日志

    CreateDefaultBuilder(args) 中默认添加了 ConsoleLoggerProvider,但是在某些场景如果不希望在命令行显示日志

    方法一,遍历所有 LoggerProvider ,找到并移除 ConsoleLoggerProvider

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, loggingBuilder)=>
            {
                // remove ConsoleLoggerProvider service in Production Environment
                if (hostingContext.HostingEnvironment.IsProduction())
                {
                    foreach (ServiceDescriptor serviceDescriptor in loggingBuilder.Services)
                    {
                        if (serviceDescriptor.ImplementationType == typeof(Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider))
                        {
                            loggingBuilder.Services.Remove(serviceDescriptor);
                            break;
                        }
                    }
                }
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
     

    方法二,清空 CreateDefaultBuilder(args) 添加的所有 LoggerProvider,然后重新添加自己需要的 LoggerProvider

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, loggingBuilder) =>
            {
                // clear all previously registered providers
                loggingBuilder.ClearProviders();
                // now register providers you need
                loggingBuilder.AddDebug();
                loggingBuilder.AddEventSourceLogger();
                if (hostingContext.HostingEnvironment.IsDevelopment())
                {
                    loggingBuilder.AddConsole();
                }
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
     
  • 相关阅读:
    oracle的常见问题与解决
    final、finally、finalize的区别
    java中读取程序运行时间
    数据库设计与SQL优化的建议
    Eclipse 快捷键操作和常用设置
    OO设计原则
    structs常用的Action
    java的深复制与浅复制
    python进制(十进制,八进制,十六进制)
    linux的shell基础
  • 原文地址:https://www.cnblogs.com/kevin860/p/14087485.html
Copyright © 2011-2022 走看看