@@ -28,6 +28,7 @@ namespace Serilog
2828 /// </summary>
2929 public static class ConsoleLoggerConfigurationExtensions
3030 {
31+ static object DefaultSyncRoot = new object ( ) ;
3132 const string DefaultConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}" ;
3233
3334 /// <summary>
@@ -38,6 +39,9 @@ public static class ConsoleLoggerConfigurationExtensions
3839 /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
3940 /// <param name="outputTemplate">A message template describing the format used to write to the sink.
4041 /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
42+ /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
43+ /// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
44+ /// the lock is held.</param>
4145 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
4246 /// <param name="levelSwitch">A switch allowing the pass-through minimum level
4347 /// to be changed at runtime.</param>
@@ -52,7 +56,8 @@ public static LoggerConfiguration Console(
5256 IFormatProvider formatProvider = null ,
5357 LoggingLevelSwitch levelSwitch = null ,
5458 LogEventLevel ? standardErrorFromLevel = null ,
55- ConsoleTheme theme = null )
59+ ConsoleTheme theme = null ,
60+ object syncRoot = null )
5661 {
5762 if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
5863 if ( outputTemplate == null ) throw new ArgumentNullException ( nameof ( outputTemplate ) ) ;
@@ -61,8 +66,10 @@ public static LoggerConfiguration Console(
6166 ConsoleTheme . None :
6267 theme ?? SystemConsoleThemes . Literate ;
6368
69+ syncRoot = syncRoot ?? DefaultSyncRoot ;
70+
6471 var formatter = new OutputTemplateRenderer ( appliedTheme , outputTemplate , formatProvider ) ;
65- return sinkConfiguration . Sink ( new ConsoleSink ( appliedTheme , formatter , standardErrorFromLevel ) , restrictedToMinimumLevel , levelSwitch ) ;
72+ return sinkConfiguration . Sink ( new ConsoleSink ( appliedTheme , formatter , standardErrorFromLevel , syncRoot ) , restrictedToMinimumLevel , levelSwitch ) ;
6673 }
6774
6875 /// <summary>
@@ -71,6 +78,9 @@ public static LoggerConfiguration Console(
7178 /// <param name="sinkConfiguration">Logger sink configuration.</param>
7279 /// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
7380 /// control plain text formatting, use the overload that accepts an output template.</param>
81+ /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
82+ /// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
83+ /// the lock is held.</param>
7484 /// <param name="restrictedToMinimumLevel">The minimum level for
7585 /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
7686 /// <param name="levelSwitch">A switch allowing the pass-through minimum level
@@ -82,12 +92,14 @@ public static LoggerConfiguration Console(
8292 ITextFormatter formatter ,
8393 LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
8494 LoggingLevelSwitch levelSwitch = null ,
85- LogEventLevel ? standardErrorFromLevel = null )
95+ LogEventLevel ? standardErrorFromLevel = null ,
96+ object syncRoot = null )
8697 {
8798 if ( sinkConfiguration == null ) throw new ArgumentNullException ( nameof ( sinkConfiguration ) ) ;
8899 if ( formatter == null ) throw new ArgumentNullException ( nameof ( formatter ) ) ;
89100
90- return sinkConfiguration . Sink ( new ConsoleSink ( ConsoleTheme . None , formatter , standardErrorFromLevel ) , restrictedToMinimumLevel , levelSwitch ) ;
101+ syncRoot = syncRoot ?? DefaultSyncRoot ;
102+ return sinkConfiguration . Sink ( new ConsoleSink ( ConsoleTheme . None , formatter , standardErrorFromLevel , syncRoot ) , restrictedToMinimumLevel , levelSwitch ) ;
91103 }
92104 }
93105}
0 commit comments