Serilog禁用日志中的控制器名称

发布于 2025-01-23 19:43:50 字数 2628 浏览 5 评论 0原文

我已经在我的ASP.NET Core 6项目中配置了我的Serilog,如以下内容:

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

        Log.Logger = new LoggerConfiguration()
            .WriteTo.Async(x => x.Logger((configureLogger) =>
            {
                configureLogger.Filter.ByExcluding("@m like '%Service Profiler%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Finished calling trace uploader%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Uploader to be used%' ci");
                configureLogger.Filter.ByExcluding("@m like '%BasicAuthentication was not authenticated%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Microsoft.ApplicationInsights.Profiler.Core%' ci");
                configureLogger.Filter.With(new[] { new LogFilterOptions() });
                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));
                });
            }))
            .Enrich.FromLogContext()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
            .MinimumLevel.Override("System", LogEventLevel.Error)
            .MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
            .CreateBootstrapLogger();

        builder.WebHost.UseSerilog();
    }
}

public class LogFilterOptions : ILogEventFilter
{
    private static readonly HashSet<string> ignoredMessages = new HashSet<string>(StringComparer.Ordinal)
    {
        "Shox.Web.Api.Controllers"
    };

    // Allow the event to be logged if the message template isn't one we ignore
    public bool IsEnabled(LogEvent logEvent) => !ignoredMessages.Contains(logEvent.MessageTemplate.Text);
}

Whtat我想禁用Serilog以在控制台上记录以下行:

info:shox.web.api.controllers.medialialibirarycontroller

任何想法如何做? 谢谢

I have configured my Serilog in my Asp.NET Core 6 project as the following:

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

        Log.Logger = new LoggerConfiguration()
            .WriteTo.Async(x => x.Logger((configureLogger) =>
            {
                configureLogger.Filter.ByExcluding("@m like '%Service Profiler%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Finished calling trace uploader%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Uploader to be used%' ci");
                configureLogger.Filter.ByExcluding("@m like '%BasicAuthentication was not authenticated%' ci");
                configureLogger.Filter.ByExcluding("@m like '%Microsoft.ApplicationInsights.Profiler.Core%' ci");
                configureLogger.Filter.With(new[] { new LogFilterOptions() });
                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));
                });
            }))
            .Enrich.FromLogContext()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
            .MinimumLevel.Override("System", LogEventLevel.Error)
            .MinimumLevel.Override("System.Net.Http.HttpClient", LogEventLevel.Warning)
            .CreateBootstrapLogger();

        builder.WebHost.UseSerilog();
    }
}

public class LogFilterOptions : ILogEventFilter
{
    private static readonly HashSet<string> ignoredMessages = new HashSet<string>(StringComparer.Ordinal)
    {
        "Shox.Web.Api.Controllers"
    };

    // Allow the event to be logged if the message template isn't one we ignore
    public bool IsEnabled(LogEvent logEvent) => !ignoredMessages.Contains(logEvent.MessageTemplate.Text);
}

Whtat I want to disable the serilog to log the following line at the console:

info: Shox.Web.Api.Controllers.MediaLibraryController

Any idea how to do that?
thx

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

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

发布评论

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

评论(1

风向决定发型 2025-01-30 19:43:50

从控制台模板{SourceContext}中删除解决了我的问题。

Removing from the console template {SourceContext} solved my problem.

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