使用Serilog在Azure上使用Serilog的过滤身份验证消息

发布于 2025-01-20 16:55:32 字数 2088 浏览 4 评论 0原文

我需要使用 Serilog 在 Azure 上过滤以下消息: AuthenticationScheme:BasicAuthentication 已成功验证。 该消息为信息级别。

在下面的代码中,我输入了到目前为止我尝试过的所有内容,并且仍然收到这些日志。 我在 asp.net 项目中使用 .net 6。

public static void Configure(WebApplicationBuilder builder)
        {
            if(builder is null)
                return;

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

            builder.WebHost.UseSerilog();
        }

在本地,这很完美。当我记录如下信息时:

logger.LogInformation("AuthenticationScheme: BasicAuthentication".ToUpper());

过滤器应用于本地,但不在 Azure 上。 有什么想法吗? 谢谢

I need to filter the following message on Azure using Serilog:
AuthenticationScheme: BasicAuthentication was successfully authenticated. The message is Information level.

In the below code I put everything I tried so far, and am still getting those logs.
I am using .net 6 in asp.net project.

public static void Configure(WebApplicationBuilder builder)
        {
            if(builder is null)
                return;

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

            builder.WebHost.UseSerilog();
        }

In local this works perfect. When the I log an information like this:

logger.LogInformation("AuthenticationScheme: BasicAuthentication".ToUpper());

the filter is applied in local, but not on Azure.
Any idea?
thnx

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

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

发布评论

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

评论(1

屋檐 2025-01-27 16:55:32

使用 serilog.expressions

   .Filter.ByExcluding("@m like '%AuthenticationScheme: BasicAuthentication%' ci")

Using Serilog.Expressions:

   .Filter.ByExcluding("@m like '%AuthenticationScheme: BasicAuthentication%' ci")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文