Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-configurable synchronized writes #66

Merged
merged 8 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Serilog.Sinks.Console/ConsoleLoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Serilog
/// </summary>
public static class ConsoleLoggerConfigurationExtensions
{
private static object DefaultSyncRoot = new object();
const string DefaultConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";

/// <summary>
Expand All @@ -38,6 +39,9 @@ public static class ConsoleLoggerConfigurationExtensions
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
/// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
/// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
/// the lock is held.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
Expand All @@ -52,7 +56,8 @@ public static LoggerConfiguration Console(
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null,
LogEventLevel? standardErrorFromLevel = null,
ConsoleTheme theme = null)
ConsoleTheme theme = null,
object syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
Expand All @@ -61,8 +66,10 @@ public static LoggerConfiguration Console(
ConsoleTheme.None :
theme ?? SystemConsoleThemes.Literate;

syncRoot = syncRoot ?? DefaultSyncRoot;

var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider);
return sinkConfiguration.Sink(new ConsoleSink(appliedTheme, formatter, standardErrorFromLevel), restrictedToMinimumLevel, levelSwitch);
return sinkConfiguration.Sink(new ConsoleSink(appliedTheme, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);
}

/// <summary>
Expand All @@ -71,6 +78,9 @@ public static LoggerConfiguration Console(
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
/// control plain text formatting, use the overload that accepts an output template.</param>
/// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
/// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
/// the lock is held.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
Expand All @@ -82,12 +92,14 @@ public static LoggerConfiguration Console(
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch levelSwitch = null,
LogEventLevel? standardErrorFromLevel = null)
LogEventLevel? standardErrorFromLevel = null,
object syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));

return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel), restrictedToMinimumLevel, levelSwitch);
syncRoot = syncRoot ?? DefaultSyncRoot;
return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);
}
}
}
6 changes: 4 additions & 2 deletions src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ConsoleSink : ILogEventSink
readonly LogEventLevel? _standardErrorFromLevel;
readonly ConsoleTheme _theme;
readonly ITextFormatter _formatter;
readonly object _syncRoot = new object();
readonly object _syncRoot;

const int DefaultWriteBufferCapacity = 256;

Expand All @@ -40,11 +40,13 @@ static ConsoleSink()
public ConsoleSink(
ConsoleTheme theme,
ITextFormatter formatter,
LogEventLevel? standardErrorFromLevel)
LogEventLevel? standardErrorFromLevel,
object syncRoot)
adamchester marked this conversation as resolved.
Show resolved Hide resolved
{
_standardErrorFromLevel = standardErrorFromLevel;
_theme = theme ?? throw new ArgumentNullException(nameof(theme));
_formatter = formatter;
_syncRoot = syncRoot ?? throw new ArgumentNullException(nameof(syncRoot));
}

public void Emit(LogEvent logEvent)
Expand Down