下面的样板程序,Microsoft为ASP.NET Core 2.1提供的C#代码应仅防止编写信息日志。但事实并非如此。
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning))
.UseStartup<Startup>();
我仍然看到类似这样的消息:
MyApp>信息:Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker [2]
和
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker:Information:执行的动作/索引在11.2286ms
我故意(暂时)删除了调用AppSettings.json的代码。为什么我仍然可以看到信息严重性日志项?
尝试在代码中进行以下更改,将第一个AddFilter的类别设置为null
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
logging.AddFilter(null, LogLevel.Warning);
})
.UseStartup<Startup>()
.ConfigureLogging(logging => logging.AddFilter("Microsoft", LogLevel.Warning));
添加logWarning以在Home / Index中进行测试
private readonly ILogger _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
_logger.LogInformation("Log Information");
_logger.LogWarning("Log Warning");
}
结果截图
很棒的代码,非常清楚。看来我们只能抑制(过滤)将在我们自己的代码中创建的日志项。我希望防止在我的代码运行时VS生成的信息性日志项。警告和更糟,很好,但并非所有其他警告。
我也在为同样的事情而苦苦挣扎。无法摆脱那些微软/系统日志。