diff --git a/Build.ps1 b/Build.ps1 index 41fbf5d..0687e20 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -25,8 +25,8 @@ foreach ($src in ls src/*) { } else { & dotnet pack -c Release --include-source -o ..\..\artifacts } - - if($LASTEXITCODE -ne 0) { exit 1 } + + if($LASTEXITCODE -ne 0) { exit 1 } Pop-Location } diff --git a/README.md b/README.md index 905fc55..9649d58 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ _Microsoft.Extensions.Logging_ provides the `BeginScope` API, which can be used Using the extension method will add a `Scope` property to your log events. This is most useful for adding simple "scope strings" to your events, as in the following code: -```cs +```csharp using (_logger.BeginScope("Transaction")) { _logger.LogInformation("Beginning..."); _logger.LogInformation("Completed in {DurationMs}ms...", 30); @@ -101,7 +101,7 @@ using (_logger.BeginScope("Transaction")) { If you simply want to add a "bag" of additional properties to your log events, however, this extension method approach can be overly verbose. For example, to add `TransactionId` and `ResponseJson` properties to your log events, you would have to do something like the following: -```cs +```csharp // WRONG! Prefer the dictionary approach below instead using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {ResponseJson}", 12345, jsonString)) { _logger.LogInformation("Completed in {DurationMs}ms...", 30); @@ -119,11 +119,12 @@ using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {Respon // } ``` -Not only does this add the unnecessary `Scope` property to your event, but it also duplicates serialized values between `Scope` and the intended properties, as you can see here with `ResponseJson`. If this were "real" JSON like an API response, then a potentially very large block of text would be duplicated within your log event! Moreover, the template string within `BeginScope` is rather arbitrary when all you want to do is add a bag of properties, and you start mixing enriching concerns with formatting concerns. +Not only does this add the unnecessary `Scope` property to your event, but it also duplicates serialized values between `Scope` and the intended properties, as you can see here with `ResponseJson`. If this were "real" JSON like an API response, then a potentially very large block of text would be duplicated within your log event! +Moreover, the template string within `BeginScope` is rather arbitrary when all you want to do is add a bag of properties, and you start mixing enriching concerns with formatting concerns. A far better alternative is to use the `BeginScope(TState state)` method. If you provide any `IEnumerable>` to this method, then Serilog will output the key/value pairs as structured properties _without_ the `Scope` property, as in this example: -```cs +```csharp var scopeProps = new Dictionary { { "TransactionId", 12345 }, { "ResponseJson", jsonString }, diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index c557b8a..8b1487b 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -13,7 +13,6 @@ - diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs index d47e84f..ca3c254 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs @@ -23,7 +23,7 @@ namespace Serilog.Extensions.Logging class CachingMessageTemplateParser { readonly MessageTemplateParser _innerParser = new MessageTemplateParser(); - + readonly object _templatesLock = new object(); readonly Hashtable _templates = new Hashtable(); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs index 80ba75f..78d72dd 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs @@ -103,7 +103,7 @@ void Write(LogEventLevel level, EventId eventId, TState state, Exception { if (logger.BindProperty(property.Key, property.Value, false, out var bound)) properties.Add(bound); - } + } } var stateType = state.GetType(); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs index cf25f06..3ee0760 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs @@ -25,7 +25,7 @@ public class SerilogLoggerFactory : ILoggerFactory { readonly LoggerProviderCollection _providerCollection; readonly SerilogLoggerProvider _provider; - + /// /// Initializes a new instance of the class. /// diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs index dd0803c..e911611 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs @@ -30,7 +30,7 @@ public SerilogLoggerScope(SerilogLoggerProvider provider, object state, IDisposa } public SerilogLoggerScope Parent { get; } - + public void Dispose() { if (!_disposed) diff --git a/src/Serilog.Extensions.Logging/LoggerSinkConfigurationExtensions.cs b/src/Serilog.Extensions.Logging/LoggerSinkConfigurationExtensions.cs index 85064db..9e78511 100644 --- a/src/Serilog.Extensions.Logging/LoggerSinkConfigurationExtensions.cs +++ b/src/Serilog.Extensions.Logging/LoggerSinkConfigurationExtensions.cs @@ -38,7 +38,7 @@ public static class LoggerSinkConfigurationExtensions public static LoggerConfiguration Providers( this LoggerSinkConfiguration configuration, LoggerProviderCollection providers, - LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, + LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, LoggingLevelSwitch levelSwitch = null) { if (configuration == null) throw new ArgumentNullException(nameof(configuration)); diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 2318516..8ec5392 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -23,7 +23,7 @@ - + diff --git a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs index b60e7f2..d3a0eb5 100644 --- a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs +++ b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs @@ -334,7 +334,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary() { var (logger, sink) = SetUp(LogLevel.Trace); - + using (logger.BeginScope(new Dictionary {{ "@Person", new Person { FirstName = "John", LastName = "Smith" }}})) { logger.Log(LogLevel.Information, 0, TestMessage, null, null); @@ -463,7 +463,7 @@ public void LowAndHighNumberedEventIdsAreMapped(int id) var scalar = Assert.IsType(idValue); Assert.Equal(id, scalar.Value); } - + [Fact] public void MismatchedMessageTemplateParameterCountIsHandled() {