Serilog禁用日志中的控制器名称
我已经在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从控制台模板{SourceContext}中删除解决了我的问题。
Removing from the console template {SourceContext} solved my problem.