如何过滤aspnet core日志?
How to filter aspnet core logs?
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> info:
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[2]
和
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker:Information:
Executed action /Index in 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");
}
结果截图
对我来说,它使用 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);
});
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> info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[2]
和
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker:Information: Executed action /Index in 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");
}
结果截图
对我来说,它使用 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);
});