我在从 serilog 文件中排除运行状况检查时遇到问题。
Serilog.Filters.Expressions 已安装,StatusCode=200 行将被过滤。
应用设置:
"Serilog": { "MinimumLevel": { "Default": "Verbose", "Override": { "Microsoft": "Information", "Microsoft.EntityFrameworkCore": "Warning", "AspNetCore.HealthChecks.UI": "Warning", "HealthChecks": "Warning", "System": "Warning" } }, "Using": ["Serilog.Filters.Expressions"], "Filter": [ { "Name": "ByExcluding", "Args": { "expression": "EndsWith(RequestPath, '/healthcheck') and StatusCode=200" } } ], "WriteTo": [ { "Name": "Async", "Args": { "configure": [ { "Name": "File", "Args": { "path": "C:/Logs/MyApi/log.txt", "rollingInterval": "Day", "retainedFileCountLimit": 7, "buffered": true } } ] } } ] }
3.程序
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{ Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.Enrich.FromLogContext()
.WriteTo.Elasticsearch()
.ReadFrom.Configuration(hostingContext.Configuration);
Log.Information("App started");
});
日志从
2020-11-04 23:59:44.128 +01:00 [INF] 请求开始 HTTP/1.1 GET https://servicesp-rbg.company.com/MyApi/healthcheck 2020-11-04 23:59:44.128 +01 :00 [INF] 请求在 0.211 毫秒内完成 200 文本/纯文本
至
2020-11-06 14:27:32.787 +01:00 [INF] 请求开始 HTTP/1.1 GET https://servicesp-rbg.company.com/My.Api/healthcheck
所以 statuscode=200 过滤器正在工作,但不是 RequestPath 'healthcheck'。