Serilog 过滤器未过滤 ResultDescription

发布于 2025-01-19 21:39:51 字数 1126 浏览 2 评论 0原文

我在使用 Serilog 过滤 ResultDescription 字段时遇到问题。可能是我做错了什么。 这是我的代码:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Async(x => x.Logger( (configureLogger) =>
    {
        configureLogger.Filter.ByExcluding("ResultDescription like '%Tesz log%'");
    }))
    .WriteTo.Async(x => x.Console(
        outputTemplate: "bla -> [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
        theme: AnsiConsoleTheme.Code))
    .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);

我还在 ByExclusion 中尝试了 lambda 表达式,但这也不起作用。 在我的控制器中,我正在测试以下内容:

public async Task<IActionResult> Test()
{
    logger.LogInformation("Teszt log!");  //I don't want to see this logs
    logger.LogInformation("I am logging!");
    return Ok("Test log has benn colled.");
}

I have problem with filtering ResultDescription field with Serilog. It might be that I am doing something wrong.
Here is my code:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Async(x => x.Logger( (configureLogger) =>
    {
        configureLogger.Filter.ByExcluding("ResultDescription like '%Tesz log%'");
    }))
    .WriteTo.Async(x => x.Console(
        outputTemplate: "bla -> [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
        theme: AnsiConsoleTheme.Code))
    .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);

I also tried a lambda expression in ByExcluding, but that didn't work either.
In my controller I am testing with the following:

public async Task<IActionResult> Test()
{
    logger.LogInformation("Teszt log!");  //I don't want to see this logs
    logger.LogInformation("I am logging!");
    return Ok("Test log has benn colled.");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2025-01-26 21:39:51
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(x => x.Logger((configureLogger) =>
{
    configureLogger.Filter.ByExcluding(logEvent => !logEvent.MessageTemplate.Text.ToLower().Contains("authenticationscheme: basicauthentication"));
    configureLogger.WriteTo.Async(x => x.Console(new ExpressionTemplate(
        template: "{@l:w4}: {SourceContext}\n" +
                      "{#if Scope is not null}" +
                    "\t{#each s in Scope}=> {s}{#delimit} {#end}\n" +
                    "{#end}" +
                    "\t {@m}\n" +
                    "{@x}",
        theme: TemplateTheme.Code)));
}))

.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Async(x => x.Logger((configureLogger) =>
{
    configureLogger.Filter.ByExcluding(logEvent => !logEvent.MessageTemplate.Text.ToLower().Contains("authenticationscheme: basicauthentication"));
    configureLogger.WriteTo.Async(x => x.Console(new ExpressionTemplate(
        template: "{@l:w4}: {SourceContext}\n" +
                      "{#if Scope is not null}" +
                    "\t{#each s in Scope}=> {s}{#delimit} {#end}\n" +
                    "{#end}" +
                    "\t {@m}\n" +
                    "{@x}",
        theme: TemplateTheme.Code)));
}))

.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.CreateLogger();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文