1- using System . Diagnostics ;
2- using Elastic . CommonSchema . Serilog ;
1+ using Elastic . CommonSchema . Serilog ;
32using Microsoft . AspNetCore . Builder ;
43using Microsoft . Extensions . DependencyInjection ;
54using Microsoft . Extensions . Hosting ;
@@ -17,22 +16,23 @@ public static class SerilogExtensions
1716{
1817 public static WebApplicationBuilder AddSerilog ( this WebApplicationBuilder builder ,
1918 LogBackend logBackend ,
19+ Dictionary < string , string > ? logAdditionalProperties = null ,
2020 int daysToRetain = 7 ,
21- Dictionary < string , string > ? valueByNameRepeatedLog = null )
21+ bool asyncSinks = false )
2222 {
2323 builder . Logging . ClearProviders ( ) ;
2424
2525 var loggerConfig = new LoggerConfiguration ( )
2626 . FilterOutUnwantedLogs ( )
2727 . Enrich
2828 . FromLogContext ( )
29- . ConfigureDestinations ( builder , logBackend )
29+ . ConfigureDestinations ( builder , logBackend , asyncSinks )
3030 . ReadFrom
3131 . Configuration ( builder . Configuration ) ;
3232
33- if ( valueByNameRepeatedLog is not null )
33+ if ( logAdditionalProperties is not null )
3434 {
35- foreach ( var ( key , value ) in valueByNameRepeatedLog )
35+ foreach ( var ( key , value ) in logAdditionalProperties )
3636 {
3737 loggerConfig . Enrich . WithProperty ( key , value ) ;
3838 }
@@ -60,30 +60,47 @@ public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builde
6060
6161 private static LoggerConfiguration ConfigureDestinations ( this LoggerConfiguration loggerConfig ,
6262 WebApplicationBuilder builder ,
63- LogBackend logBackend )
63+ LogBackend logBackend ,
64+ bool asyncSinks )
6465 {
6566 if ( builder . Environment . IsLocal ( ) )
6667 {
67- loggerConfig . WriteTo . Async ( a => a . Console ( ) ) ;
68+ loggerConfig . WriteToConsole ( asyncSinks ) ;
69+
6870 return loggerConfig ;
6971 }
7072
7173 if ( ! builder . Environment . IsProduction ( ) )
7274 {
73- loggerConfig . WriteTo . Async ( a => a . Console ( ) ) ;
75+ loggerConfig . WriteToConsole ( asyncSinks ) ;
7476 }
7577
7678 if ( logBackend != LogBackend . None )
7779 {
78- loggerConfig . WriteToFileAsync ( builder , logBackend ) ;
80+ loggerConfig . WriteToFile ( builder , logBackend , asyncSinks ) ;
7981 }
8082
8183 return loggerConfig ;
8284 }
8385
84- private static LoggerConfiguration WriteToFileAsync ( this LoggerConfiguration loggerConfig ,
86+ private static LoggerConfiguration WriteToConsole ( this LoggerConfiguration loggerConfig , bool useAsync )
87+ {
88+ if ( useAsync )
89+ {
90+ loggerConfig . WriteTo . Async ( a => a . Console ( ) ) ;
91+ }
92+ else
93+ {
94+ loggerConfig . WriteTo . Console ( ) ;
95+ }
96+
97+ return loggerConfig ;
98+ }
99+
100+ private static LoggerConfiguration WriteToFile ( this LoggerConfiguration loggerConfig ,
85101 WebApplicationBuilder builder ,
86- LogBackend logBackend )
102+ LogBackend logBackend ,
103+ bool useAsync )
87104 {
88105 // Choose the formatter based on the selected log backend
89106 ITextFormatter formatter = logBackend switch
@@ -94,11 +111,18 @@ private static LoggerConfiguration WriteToFileAsync(this LoggerConfiguration log
94111 _ => new CompactJsonFormatter ( ) // Fallback
95112 } ;
96113
97- return loggerConfig . WriteTo . Async ( a =>
98- a . File ( formatter ,
99- builder . GetLogsPath ( ) ,
100- rollingInterval : RollingInterval . Day )
101- ) ;
114+ var logPath = builder . GetLogsPath ( ) ;
115+
116+ if ( useAsync )
117+ {
118+ return loggerConfig . WriteTo . Async ( a =>
119+ a . File ( formatter ,
120+ logPath ,
121+ rollingInterval : RollingInterval . Day )
122+ ) ;
123+ }
124+
125+ return loggerConfig . WriteTo . File ( formatter , logPath , rollingInterval : RollingInterval . Day ) ;
102126 }
103127
104128 #region Filtering
0 commit comments