Skip to content

Commit 3f63b63

Browse files
authored
Merge pull request #214 from sungam3r/net7
Migrate to NET7 and C#11
2 parents da7b61b + a50d9e6 commit 3f63b63

31 files changed

+1646
-1692
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

Directory.Build.props

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
<ImplicitUsings>enable</ImplicitUsings>
11+
</PropertyGroup>
12+
13+
</Project>

Directory.Build.targets

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

global.json

-7
This file was deleted.

samples/Sample/Program.cs

+66-68
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,77 @@
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
6+
namespace Sample;
7+
8+
public class Program
89
{
9-
public class Program
10+
public static void Main(string[] args)
1011
{
11-
public static void Main(string[] args)
12+
// Creating a `LoggerProviderCollection` lets Serilog optionally write
13+
// events through other dynamically-added MEL ILoggerProviders.
14+
var providers = new LoggerProviderCollection();
15+
16+
Log.Logger = new LoggerConfiguration()
17+
.MinimumLevel.Debug()
18+
.WriteTo.Console()
19+
.WriteTo.Providers(providers)
20+
.CreateLogger();
21+
22+
var services = new ServiceCollection();
23+
24+
services.AddSingleton(providers);
25+
services.AddSingleton<ILoggerFactory>(sc =>
26+
{
27+
var providerCollection = sc.GetService<LoggerProviderCollection>();
28+
var factory = new SerilogLoggerFactory(null, true, providerCollection);
29+
30+
foreach (var provider in sc.GetServices<ILoggerProvider>())
31+
factory.AddProvider(provider);
32+
33+
return factory;
34+
});
35+
36+
services.AddLogging(l => l.AddConsole());
37+
38+
var serviceProvider = services.BuildServiceProvider();
39+
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
40+
41+
var startTime = DateTimeOffset.UtcNow;
42+
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);
43+
44+
try
45+
{
46+
throw new Exception("Boom!");
47+
}
48+
catch (Exception ex)
49+
{
50+
logger.LogCritical("Unexpected critical error starting application", ex);
51+
logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null!);
52+
// This write should not log anything
53+
logger.Log<object>(LogLevel.Critical, 0, null!, null, null!);
54+
logger.LogError("Unexpected error", ex);
55+
logger.LogWarning("Unexpected warning", ex);
56+
}
57+
58+
using (logger.BeginScope("Main"))
1259
{
13-
// Creating a `LoggerProviderCollection` lets Serilog optionally write
14-
// events through other dynamically-added MEL ILoggerProviders.
15-
var providers = new LoggerProviderCollection();
16-
17-
Log.Logger = new LoggerConfiguration()
18-
.MinimumLevel.Debug()
19-
.WriteTo.Console()
20-
.WriteTo.Providers(providers)
21-
.CreateLogger();
22-
23-
var services = new ServiceCollection();
24-
25-
services.AddSingleton(providers);
26-
services.AddSingleton<ILoggerFactory>(sc =>
27-
{
28-
var providerCollection = sc.GetService<LoggerProviderCollection>();
29-
var factory = new SerilogLoggerFactory(null, true, providerCollection);
30-
31-
foreach (var provider in sc.GetServices<ILoggerProvider>())
32-
factory.AddProvider(provider);
33-
34-
return factory;
35-
});
36-
37-
services.AddLogging(l => l.AddConsole());
38-
39-
var serviceProvider = services.BuildServiceProvider();
40-
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
41-
42-
var startTime = DateTimeOffset.UtcNow;
43-
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);
44-
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-
}
58-
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-
}
65-
66-
var endTime = DateTimeOffset.UtcNow;
67-
logger.LogInformation(2, "Stopping at {StopTime}", endTime);
68-
69-
logger.LogInformation("Stopping");
70-
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);
75-
76-
serviceProvider.Dispose();
60+
logger.LogInformation("Waiting for user input");
61+
var key = Console.Read();
62+
logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key });
7763
}
64+
65+
var endTime = DateTimeOffset.UtcNow;
66+
logger.LogInformation(2, "Stopping at {StopTime}", endTime);
67+
68+
logger.LogInformation("Stopping");
69+
70+
logger.LogInformation(Environment.NewLine);
71+
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
72+
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------");
73+
logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
74+
75+
serviceProvider.Dispose();
7876
}
7977
}

samples/Sample/Sample.csproj

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
4+
<TargetFramework>net7</TargetFramework>
55
<AssemblyName>Sample</AssemblyName>
66
<OutputType>Exe</OutputType>
7-
<PackageId>Sample</PackageId>
87
</PropertyGroup>
98

109
<ItemGroup>
1110
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
1211
</ItemGroup>
1312

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

2019
</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

serilog-extensions-logging.sln.DotSettings

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Serilog Contributors
1+
// Copyright (c) Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -13,55 +13,53 @@
1313
// limitations under the License.
1414

1515

16-
using System;
1716
using Serilog.Events;
1817
using Serilog.Parsing;
1918
using System.Collections;
2019

21-
namespace Serilog.Extensions.Logging
20+
namespace Serilog.Extensions.Logging;
21+
22+
class CachingMessageTemplateParser
2223
{
23-
class CachingMessageTemplateParser
24-
{
25-
readonly MessageTemplateParser _innerParser = new MessageTemplateParser();
24+
readonly MessageTemplateParser _innerParser = new();
2625

27-
readonly object _templatesLock = new object();
28-
readonly Hashtable _templates = new Hashtable();
26+
readonly object _templatesLock = new();
27+
readonly Hashtable _templates = new();
2928

30-
const int MaxCacheItems = 1000;
31-
const int MaxCachedTemplateLength = 1024;
29+
const int MaxCacheItems = 1000;
30+
const int MaxCachedTemplateLength = 1024;
3231

33-
public MessageTemplate Parse(string messageTemplate)
34-
{
35-
if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate));
36-
37-
if (messageTemplate.Length > MaxCachedTemplateLength)
38-
return _innerParser.Parse(messageTemplate);
32+
public MessageTemplate Parse(string messageTemplate)
33+
{
34+
if (messageTemplate == null) throw new ArgumentNullException(nameof(messageTemplate));
3935

40-
// ReSharper disable once InconsistentlySynchronizedField
41-
// ignored warning because this is by design
42-
var result = (MessageTemplate)_templates[messageTemplate];
43-
if (result != null)
44-
return result;
36+
if (messageTemplate.Length > MaxCachedTemplateLength)
37+
return _innerParser.Parse(messageTemplate);
4538

46-
result = _innerParser.Parse(messageTemplate);
39+
// ReSharper disable once InconsistentlySynchronizedField
40+
// ignored warning because this is by design
41+
var result = (MessageTemplate)_templates[messageTemplate];
42+
if (result != null)
43+
return result;
4744

48-
lock (_templatesLock)
49-
{
50-
// Exceeding MaxCacheItems is *not* the sunny day scenario; all we're doing here is preventing out-of-memory
51-
// conditions when the library is used incorrectly. Correct use (templates, rather than
52-
// direct message strings) should barely, if ever, overflow this cache.
45+
result = _innerParser.Parse(messageTemplate);
5346

54-
// Changing workloads through the lifecycle of an app instance mean we can gain some ground by
55-
// potentially dropping templates generated only in startup, or only during specific infrequent
56-
// activities.
47+
lock (_templatesLock)
48+
{
49+
// Exceeding MaxCacheItems is *not* the sunny day scenario; all we're doing here is preventing out-of-memory
50+
// conditions when the library is used incorrectly. Correct use (templates, rather than
51+
// direct message strings) should barely, if ever, overflow this cache.
5752

58-
if (_templates.Count == MaxCacheItems)
59-
_templates.Clear();
53+
// Changing workloads through the lifecycle of an app instance mean we can gain some ground by
54+
// potentially dropping templates generated only in startup, or only during specific infrequent
55+
// activities.
6056

61-
_templates[messageTemplate] = result;
62-
}
57+
if (_templates.Count == MaxCacheItems)
58+
_templates.Clear();
6359

64-
return result;
60+
_templates[messageTemplate] = result;
6561
}
62+
63+
return result;
6664
}
6765
}

0 commit comments

Comments
 (0)