下面的样板程序,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:信息: 在11.2286ms内执行动作/ Index
我故意(暂时)删除了调用AppSettings.json的代码。为什么我仍会看到信息严重性日志项?
答案 0 :(得分:1)
尝试在代码中进行以下更改,将第一个AddFilter的类别设置为空
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以便在首页/索引中进行测试
private readonly ILogger _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
_logger.LogInformation("Log Information");
_logger.LogWarning("Log Warning");
}
结果的屏幕截图
答案 1 :(得分:1)
对我来说,它使用AddFilter <>()通用方法(我正在使用ASP.Net Core 3.1)。我正在记录Windows事件日志,我只想警告和错误在那里。 因此,我在EventLogLoggerProvider中添加了一个过滤器,以仅记录来自“ Microsoft”类别的警告。
services.AddLogging(logging =>
{
logging.AddEventLog(eventLogSettings =>
{
eventLogSettings.LogName = Constants.Service.EventLogName;
eventLogSettings.SourceName = Constants.Service.EventSourceName;
});
logging.AddFilter<EventLogLoggerProvider>("Microsoft", LogLevel.Warning);
});