'Serilog disable the controller name in logs
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
Solution 1:[1]
Removing from the console template {SourceContext} solved my problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wasyster |
