Skip to content

Commit da7b61b

Browse files
authored
Merge pull request #210 from sungam3r/cleanup
Little cleanup
2 parents 6539e2d + 1269b85 commit da7b61b

File tree

9 files changed

+14
-14
lines changed

9 files changed

+14
-14
lines changed

Build.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ foreach ($src in ls src/*) {
2525
} else {
2626
& dotnet pack -c Release --include-source -o ..\..\artifacts
2727
}
28-
29-
if($LASTEXITCODE -ne 0) { exit 1 }
28+
29+
if($LASTEXITCODE -ne 0) { exit 1 }
3030

3131
Pop-Location
3232
}

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ _Microsoft.Extensions.Logging_ provides the `BeginScope` API, which can be used
8989

9090
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:
9191

92-
```cs
92+
```csharp
9393
using (_logger.BeginScope("Transaction")) {
9494
_logger.LogInformation("Beginning...");
9595
_logger.LogInformation("Completed in {DurationMs}ms...", 30);
@@ -101,7 +101,7 @@ using (_logger.BeginScope("Transaction")) {
101101

102102
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:
103103

104-
```cs
104+
```csharp
105105
// WRONG! Prefer the dictionary approach below instead
106106
using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {ResponseJson}", 12345, jsonString)) {
107107
_logger.LogInformation("Completed in {DurationMs}ms...", 30);
@@ -119,11 +119,12 @@ using (_logger.BeginScope("TransactionId: {TransactionId}, ResponseJson: {Respon
119119
// }
120120
```
121121

122-
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.
122+
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!
123+
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.
123124

124125
A far better alternative is to use the `BeginScope<TState>(TState state)` method. If you provide any `IEnumerable<KeyValuePair<string, object>>` to this method, then Serilog will output the key/value pairs as structured properties _without_ the `Scope` property, as in this example:
125126

126-
```cs
127+
```csharp
127128
var scopeProps = new Dictionary<string, object> {
128129
{ "TransactionId", 12345 },
129130
{ "ResponseJson", jsonString },

samples/Sample/Sample.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
16-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
1716
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
1817
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
1918
</ItemGroup>

src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Serilog.Extensions.Logging
2323
class CachingMessageTemplateParser
2424
{
2525
readonly MessageTemplateParser _innerParser = new MessageTemplateParser();
26-
26+
2727
readonly object _templatesLock = new object();
2828
readonly Hashtable _templates = new Hashtable();
2929

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
103103
{
104104
if (logger.BindProperty(property.Key, property.Value, false, out var bound))
105105
properties.Add(bound);
106-
}
106+
}
107107
}
108108

109109
var stateType = state.GetType();

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SerilogLoggerFactory : ILoggerFactory
2525
{
2626
readonly LoggerProviderCollection _providerCollection;
2727
readonly SerilogLoggerProvider _provider;
28-
28+
2929
/// <summary>
3030
/// Initializes a new instance of the <see cref="SerilogLoggerFactory"/> class.
3131
/// </summary>

src/Serilog.Extensions.Logging/LoggerSinkConfigurationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static class LoggerSinkConfigurationExtensions
3838
public static LoggerConfiguration Providers(
3939
this LoggerSinkConfiguration configuration,
4040
LoggerProviderCollection providers,
41-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
41+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
4242
LoggingLevelSwitch levelSwitch = null)
4343
{
4444
if (configuration == null) throw new ArgumentNullException(nameof(configuration));

src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<None Include="..\..\assets\serilog-extension-nuget.png" Pack="true" PackagePath="" />
26+
<None Include="..\..\assets\serilog-extension-nuget.png" Pack="true" PackagePath="" Visible="false" />
2727
</ItemGroup>
2828

2929
<ItemGroup>

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate
334334
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
335335
{
336336
var (logger, sink) = SetUp(LogLevel.Trace);
337-
337+
338338
using (logger.BeginScope(new Dictionary<string, object> {{ "@Person", new Person { FirstName = "John", LastName = "Smith" }}}))
339339
{
340340
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
@@ -463,7 +463,7 @@ public void LowAndHighNumberedEventIdsAreMapped(int id)
463463
var scalar = Assert.IsType<ScalarValue>(idValue);
464464
Assert.Equal(id, scalar.Value);
465465
}
466-
466+
467467
[Fact]
468468
public void MismatchedMessageTemplateParameterCountIsHandled()
469469
{

0 commit comments

Comments
 (0)