Title: Serilog builder.Host and AddFilter
Tags: asp.net-core;serilog;.net-6.0;serilog-filter
Question: I have a a webapp using the terse .NET 6/7 serilog initialization:
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((ctx, lc) =&gt; {
lc.WriteTo.File(
logDir + &quot;\\mylog.log&quot;
).Filter.ByExcluding(le =&gt; {
var s = le.RenderMessage();
return s.StartsWith(&quot;Executing endpoint&quot;)
|| s.StartsWith(&quot;Executed endpoint&quot;)
|| s.StartsWith(&quot;User logged in.&quot;);
I now want to filter some messages:
AddFilter(&quot;Microsoft.AspNetCore.SignalR&quot;, LogLevel.Debug);
AddFilter(&quot;Microsoft.AspNetCore.Http.Connections&quot;, LogLevel.Debug);
However I am not sure how to plug these AddFilter statements into the above builder
Any help ?
Comment: thanks @NicholasBlumhardt , bad copy&paste .. fixed!

Comment: Hi! Your first code sample doesn't look right - there's no `le` in the `UseSerilog()` configuration callback argument list.

Here is the accepted answer: For a code based approach, use MinimumLevel.Override
builder.UseSerilog(
(ctx, lc) =&gt; lc
.WriteTo.File(logDir + &quot;\\mylog.log&quot;)
.Filter.ByExcluding(le =&gt; {
var s = le.RenderMessage();
return s.StartsWith(&quot;Executing endpoint&quot;)
|| s.StartsWith(&quot;Executed endpoint&quot;)
|| s.StartsWith(&quot;User logged in.&quot;);
})
.MinimumLevel.Override(&quot;Microsoft.AspNetCore.SignalR&quot;, Serilog.Events.LogEventLevel.Debug)
.MinimumLevel.Override(&quot;Microsoft.AspNetCore.Http.Connections&quot;, Serilog.Events.LogEventLevel.Debug)
);
For a configuration approach, use ReadFrom.Configuration
builder.UseSerilog(
(ctx, lc) =&gt; lc
.WriteTo.File(logDir + &quot;\\mylog.log&quot;)
.Filter.ByExcluding(le =&gt; {
var s = le.RenderMessage();
return s.StartsWith(&quot;Executing endpoint&quot;)
|| s.StartsWith(&quot;Executed endpoint&quot;)
|| s.StartsWith(&quot;User logged in.&quot;);
})
.ReadFrom.Configuration(ctx.Configuration)
);
Your appsettings.json file should look something like below.
The log levels might be different.
&quot;Logging&quot;: {
&quot;LogLevel&quot;: {
&quot;Default&quot;: &quot;Warning&quot;,
&quot;Microsoft&quot;: &quot;Warning&quot;,
&quot;Microsoft.Hosting.Lifetime&quot;: &quot;Warning&quot;
}
},
&quot;Serilog&quot;: {
&quot;MinimumLevel&quot;: {
&quot;Default&quot;: &quot;Warning&quot;,
&quot;Override&quot;: {
&quot;Microsoft.AspNetCore.SignalR&quot;: &quot;Debug&quot;,
&quot;Microsoft.AspNetCore.Http.Connections&quot;: &quot;Debug&quot;
}
}
Comment for this answer: great thank you ! is there a serilog documentation for this .MinimumLevel.Override anywhere ? can't fine it
Comment for this answer: @kofifus There is indeed not much information to find. I learned about it from a unit test in the [source code](https://github.com/serilog/serilog/blob/426781085d3eb05afe6b56e22b5df8ebe7ddbaf9/test/Serilog.Tests/Core/ChildLoggerKnownLimitationsTests.cs#L23). For open source projects, the source code often is the best place to get ideas.

Here is another answer: If you want to omit the Microsoft.Namespace log. The JSON configuration to do this looks like this：
You need to install Serilog.Filters.Expressions nuget package
&quot;Serilog&quot;: {
&quot;MinimumLevel&quot;: {
&quot;Default&quot;: &quot;Debug&quot;,
&quot;Override&quot;: {
&quot;Microsoft&quot;: &quot;Warning&quot;,
&quot;System&quot;: &quot;Warning&quot;,
&quot;Microsoft.EntityFrameworkCore&quot;: &quot;Information&quot;
}
},
&quot;WriteTo&quot;: [
{
&quot;Name&quot;: &quot;Logger&quot;,
&quot;Args&quot;: {
&quot;configureLogger&quot;: {
&quot;Filter&quot;: [
{
&quot;Name&quot;: &quot;ByIncludingOnly&quot;,
&quot;Args&quot;: {
&quot;expression&quot;: &quot;StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')&quot;
}
}
],
&quot;WriteTo&quot;: [
{
&quot;Name&quot;: &quot;File&quot;,
&quot;Args&quot;: {
&quot;path&quot;: &quot;C:\\LOGS\\TestService_EF_.json&quot;,
&quot;outputTemplate&quot;: &quot;{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}&quot;,
&quot;rollingInterval&quot;: &quot;Day&quot;,
&quot;retainedFileCountLimit&quot;: 7
}
}
]
}
}
}
Comment for this answer: Thank you! can that be done in code rather than JSON (ie call AddFilter somehow ?)
Comment for this answer: You can read this article, maybe it will help you: https://nblumhardt.com/2017/01/serilog-filtering-dsl/

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:03/06/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net-core><serilog><.net-6.0><serilog-filter>', 'Title': 'Serilog builder.Host and AddFilter', 'ViewCount': 306, 'id': 71374831, 'user_AboutMe': '', 'user_AccountId': 209189.0, 'user_DisplayName': 'kofifus', 'user_Reputation': 15163.0, 'user_Views': 1131.0, 'user_id': 460084.0}}