Skip to content

Commit 710777b

Browse files
authored
Merge pull request #223 from serilog/dev
7.0.0 Release
2 parents cd95c61 + b524ab6 commit 710777b

38 files changed

+1846
-1679
lines changed

.editorconfig

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
root = true
2+
3+
[*]
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4
8+
charset = utf-8
9+
end_of_line = lf
10+
11+
[*.{csproj,json,config,yml,props}]
12+
indent_size = 2
13+
14+
[*.sh]
15+
end_of_line = lf
16+
17+
[*.{cmd, bat}]
18+
end_of_line = crlf
19+
20+
# C# formatting settings - Namespace options
21+
csharp_style_namespace_declarations = file_scoped:suggestion
22+
23+
csharp_style_prefer_switch_expression = true:suggestion

Build.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if(Test-Path .\artifacts) {
1111

1212
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
1313
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
14-
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"]
1515

1616
echo "build: Version suffix is $suffix"
1717

@@ -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
}

Directory.Build.props

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<LangVersion>latest</LangVersion>
5+
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
6+
<SignAssembly>true</SignAssembly>
7+
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)assets/Serilog.snk</AssemblyOriginatorKeyFile>
8+
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
9+
<Nullable>enable</Nullable>
10+
</PropertyGroup>
11+
12+
</Project>

Directory.Build.targets

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project>
2+
3+
</Project>

README.md

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Serilog.Extensions.Logging [![Build status](https://ci.appveyor.com/api/projects/status/865nohxfiq1rnby0/branch/master?svg=true)](https://ci.appveyor.com/project/serilog/serilog-framework-logging/history) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Extensions.Logging.svg?style=flat)](https://www.nuget.org/packages/Serilog.Extensions.Logging/)
1+
# Serilog.Extensions.Logging [![Build status](https://ci.appveyor.com/api/projects/status/865nohxfiq1rnby0/branch/master?svg=true)](https://ci.appveyor.com/project/serilog/serilog-framework-logging/history) [![NuGet Version](http://img.shields.io/nuget/v/Serilog.Extensions.Logging.svg?style=flat)](https://www.nuget.org/packages/Serilog.Extensions.Logging/)
22

33
A Serilog provider for [Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging), the logging subsystem used by ASP.NET Core.
44

@@ -16,9 +16,9 @@ The package implements `AddSerilog()` on `ILoggingBuilder` and `ILoggerFactory`
1616

1717
**First**, install the _Serilog.Extensions.Logging_ [NuGet package](https://www.nuget.org/packages/Serilog.Extensions.Logging) into your web or console app. You will need a way to view the log messages - _Serilog.Sinks.Console_ writes these to the console.
1818

19-
```powershell
20-
Install-Package Serilog.Extensions.Logging -DependencyVersion Highest
21-
Install-Package Serilog.Sinks.Console
19+
```sh
20+
dotnet add package Serilog.Extensions.Logging
21+
dotnet add package Serilog.Sinks.Console
2222
```
2323

2424
**Next**, in your application's `Startup` method, configure Serilog first:
@@ -34,7 +34,7 @@ public class Startup
3434
.Enrich.FromLogContext()
3535
.WriteTo.Console()
3636
.CreateLogger();
37-
37+
3838
// Other startup code
3939
```
4040

@@ -46,7 +46,7 @@ call `AddSerilog()` on the provided `loggingBuilder`.
4646
{
4747
services.AddLogging(loggingBuilder =>
4848
loggingBuilder.AddSerilog(dispose: true));
49-
49+
5050
// Other services ...
5151
}
5252
```
@@ -60,7 +60,7 @@ call `AddSerilog()` on the provided `loggingBuilder`.
6060
IApplicationLifetime appLifetime)
6161
{
6262
loggerfactory.AddSerilog();
63-
63+
6464
// Ensure any buffered events are sent at shutdown
6565
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
6666
```
@@ -69,7 +69,7 @@ That's it! With the level bumped up a little you should see log output like:
6969

7070
```
7171
[22:14:44.646 DBG] RouteCollection.RouteAsync
72-
Routes:
72+
Routes:
7373
Microsoft.AspNet.Mvc.Routing.AttributeRoute
7474
{controller=Home}/{action=Index}/{id?}
7575
Handled? True
@@ -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 },
@@ -143,6 +144,10 @@ using (_logger.BeginScope(scopeProps) {
143144
// }
144145
```
145146

147+
### Versioning
148+
149+
This package tracks the versioning and target framework support of its [_Microsoft.Extensions.Logging_](https://nuget.org/packages/Microsoft.Extensions.Logging) dependency.
150+
146151
### Credits
147152

148153
This package evolved from an earlier package _Microsoft.Framework.Logging.Serilog_ [provided by the ASP.NET team](https://github.com/aspnet/Logging/pull/182).

appveyor.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2019
3+
image: Visual Studio 2022
44
install:
55
- ps: mkdir -Force ".\build\" | Out-Null
66
build_script:
@@ -11,15 +11,15 @@ artifacts:
1111
deploy:
1212
- provider: NuGet
1313
api_key:
14-
secure: kYR3BYzJm3wSFbFjPhgTzuDHHcE8ApoNUmHvJvunWZ39nyrqVk6J6srjzYVQ7/gX
14+
secure: EN9f+XXE3fW+ebL4wxrIbafdtbNvRfddBN8UUixvctYh4qMBHzr1JdnM83QsM1zo
1515
skip_symbols: true
1616
on:
17-
branch: /^(master|dev)$/
17+
branch: /^(main|dev)$/
1818
- provider: GitHub
1919
auth_token:
2020
secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX
2121
artifact: /Serilog.*\.nupkg/
2222
tag: v$(appveyor_build_version)
2323
on:
24-
branch: master
24+
branch: main
2525

global.json

-7
This file was deleted.

samples/Sample/Program.cs

+49-60
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,68 @@
1-
using System;
21
using Microsoft.Extensions.DependencyInjection;
32
using Microsoft.Extensions.Logging;
43
using Serilog;
54
using Serilog.Extensions.Logging;
65

7-
namespace Sample
8-
{
9-
public class Program
10-
{
11-
public static void Main(string[] args)
12-
{
13-
// Creating a `LoggerProviderCollection` lets Serilog optionally write
14-
// events through other dynamically-added MEL ILoggerProviders.
15-
var providers = new LoggerProviderCollection();
6+
// Creating a `LoggerProviderCollection` lets Serilog optionally write
7+
// events through other dynamically-added MEL ILoggerProviders.
8+
var providers = new LoggerProviderCollection();
169

17-
Log.Logger = new LoggerConfiguration()
18-
.MinimumLevel.Debug()
19-
.WriteTo.Console()
20-
.WriteTo.Providers(providers)
21-
.CreateLogger();
10+
Log.Logger = new LoggerConfiguration()
11+
.MinimumLevel.Debug()
12+
.WriteTo.Console()
13+
.WriteTo.Providers(providers)
14+
.CreateLogger();
2215

23-
var services = new ServiceCollection();
16+
var services = new ServiceCollection();
2417

25-
services.AddSingleton(providers);
26-
services.AddSingleton<ILoggerFactory>(sc =>
27-
{
28-
var providerCollection = sc.GetService<LoggerProviderCollection>();
29-
var factory = new SerilogLoggerFactory(null, true, providerCollection);
18+
services.AddSingleton(providers);
19+
services.AddSingleton<ILoggerFactory>(sc =>
20+
{
21+
var providerCollection = sc.GetService<LoggerProviderCollection>();
22+
var factory = new SerilogLoggerFactory(null, true, providerCollection);
3023

31-
foreach (var provider in sc.GetServices<ILoggerProvider>())
32-
factory.AddProvider(provider);
24+
foreach (var provider in sc.GetServices<ILoggerProvider>())
25+
factory.AddProvider(provider);
3326

34-
return factory;
35-
});
27+
return factory;
28+
});
3629

37-
services.AddLogging(l => l.AddConsole());
30+
services.AddLogging(l => l.AddConsole());
3831

39-
var serviceProvider = services.BuildServiceProvider();
40-
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
32+
var serviceProvider = services.BuildServiceProvider();
33+
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
4134

42-
var startTime = DateTimeOffset.UtcNow;
43-
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);
35+
var startTime = DateTimeOffset.UtcNow;
36+
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);
4437

45-
try
46-
{
47-
throw new Exception("Boom!");
48-
}
49-
catch (Exception ex)
50-
{
51-
logger.LogCritical("Unexpected critical error starting application", ex);
52-
logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null);
53-
// This write should not log anything
54-
logger.Log<object>(LogLevel.Critical, 0, null, null, null);
55-
logger.LogError("Unexpected error", ex);
56-
logger.LogWarning("Unexpected warning", ex);
57-
}
38+
try
39+
{
40+
throw new Exception("Boom!");
41+
}
42+
catch (Exception ex)
43+
{
44+
logger.LogCritical(ex, "Unexpected critical error starting application");
45+
logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null!);
46+
// This write should not log anything
47+
logger.Log<object>(LogLevel.Critical, 0, null!, null, null!);
48+
logger.LogError(ex, "Unexpected error");
49+
logger.LogWarning(ex, "Unexpected warning");
50+
}
5851

59-
using (logger.BeginScope("Main"))
60-
{
61-
logger.LogInformation("Waiting for user input");
62-
var key = Console.Read();
63-
logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key });
64-
}
52+
using (logger.BeginScope("Main"))
53+
{
54+
logger.LogInformation("Waiting for user input");
55+
var key = Console.Read();
56+
logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key });
57+
}
6558

66-
var endTime = DateTimeOffset.UtcNow;
67-
logger.LogInformation(2, "Stopping at {StopTime}", endTime);
59+
var endTime = DateTimeOffset.UtcNow;
60+
logger.LogInformation(2, "Stopping at {StopTime}", endTime);
6861

69-
logger.LogInformation("Stopping");
62+
logger.LogInformation("Stopping");
7063

71-
logger.LogInformation(Environment.NewLine);
72-
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
73-
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------");
74-
logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
64+
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
65+
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------");
66+
logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
7567

76-
serviceProvider.Dispose();
77-
}
78-
}
79-
}
68+
serviceProvider.Dispose();

samples/Sample/Sample.csproj

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
4+
<TargetFramework>net7.0</TargetFramework>
55
<AssemblyName>Sample</AssemblyName>
66
<OutputType>Exe</OutputType>
7-
<PackageId>Sample</PackageId>
7+
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
1111
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
16-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
17-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
18-
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
17+
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
1918
</ItemGroup>
2019

21-
</Project>
20+
</Project>

serilog-extensions-logging.sln

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29209.62
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33424.131
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}"
77
EndProject
@@ -17,9 +17,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "samples\Sample\Sa
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9DF-AEDD-4AA6-BEA4-912DEF3E5B8E}"
1919
ProjectSection(SolutionItems) = preProject
20+
.editorconfig = .editorconfig
2021
appveyor.yml = appveyor.yml
2122
Build.ps1 = Build.ps1
22-
global.json = global.json
23+
Directory.Build.props = Directory.Build.props
24+
Directory.Build.targets = Directory.Build.targets
2325
README.md = README.md
2426
assets\Serilog.snk = assets\Serilog.snk
2527
EndProjectSection
+1-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:Boolean x:Key="/Default/UserDictionary/Words/=destructure/@EntryIndexedValue">True</s:Boolean>
3-
<s:Boolean x:Key="/Default/UserDictionary/Words/=destructured/@EntryIndexedValue">True</s:Boolean>
4-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Destructurer/@EntryIndexedValue">True</s:Boolean>
5-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Destructures/@EntryIndexedValue">True</s:Boolean>
6-
<s:Boolean x:Key="/Default/UserDictionary/Words/=enricher/@EntryIndexedValue">True</s:Boolean>
7-
<s:Boolean x:Key="/Default/UserDictionary/Words/=enrichers/@EntryIndexedValue">True</s:Boolean>
8-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Loggable/@EntryIndexedValue">True</s:Boolean>
9-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nonscalar/@EntryIndexedValue">True</s:Boolean>
10-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
11-
<s:Boolean x:Key="/Default/UserDictionary/Words/=sobj/@EntryIndexedValue">True</s:Boolean>
12-
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stringification/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=stringified/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)