Skip to content

Commit 31451b4

Browse files
committed
v7.0 - pin to MEC v7, including matching target frameworks
1 parent 9f8f898 commit 31451b4

15 files changed

+125
-116
lines changed

sample/Sample/CustomFilter.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Serilog.Core;
2+
using Serilog.Events;
3+
4+
namespace Sample;
5+
6+
// The filter syntax in the sample configuration file is
7+
// processed by the Serilog.Filters.Expressions package.
8+
public class CustomFilter : ILogEventFilter
9+
{
10+
readonly LogEventLevel _levelFilter;
11+
12+
public CustomFilter(LogEventLevel levelFilter = LogEventLevel.Information)
13+
{
14+
_levelFilter = levelFilter;
15+
}
16+
17+
public bool IsEnabled(LogEvent logEvent)
18+
{
19+
return logEvent.Level >= _levelFilter;
20+
}
21+
}

sample/Sample/CustomPolicy.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace Sample;
6+
7+
public class CustomPolicy : IDestructuringPolicy
8+
{
9+
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
10+
{
11+
result = null;
12+
13+
if (value is LoginData loginData)
14+
{
15+
result = new StructureValue(
16+
new List<LogEventProperty>
17+
{
18+
new("Username", new ScalarValue(loginData.Username))
19+
});
20+
}
21+
22+
return (result != null);
23+
}
24+
}

sample/Sample/LoginData.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Sample;
2+
3+
public class LoginData
4+
{
5+
public string? Username;
6+
// ReSharper disable once NotAccessedField.Global
7+
public string? Password;
8+
}

sample/Sample/Program.cs

Lines changed: 27 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,46 @@
11
using Microsoft.Extensions.Configuration;
2-
2+
using Sample;
33
using Serilog;
44
using Serilog.Core;
5-
using Serilog.Events;
65
using Serilog.Debugging;
76

87
// ReSharper disable UnusedType.Global
98

10-
namespace Sample;
11-
12-
public class Program
13-
{
14-
public static void Main(string[] args)
15-
{
16-
SelfLog.Enable(Console.Error);
17-
18-
Thread.CurrentThread.Name = "Main thread";
19-
20-
var configuration = new ConfigurationBuilder()
21-
.SetBasePath(Directory.GetCurrentDirectory())
22-
.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
23-
.Build();
24-
25-
var logger = new LoggerConfiguration()
26-
.ReadFrom.Configuration(configuration)
27-
.CreateLogger();
28-
29-
logger.Information("Args: {Args}", args);
30-
31-
do
32-
{
33-
logger.ForContext<Program>().Information("Hello, world!");
34-
logger.ForContext<Program>().Error("Hello, world!");
35-
logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Warning("Hello, world!");
36-
logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Error("Hello, world!");
37-
logger.ForContext(Constants.SourceContextPropertyName, "MyApp.Something.Tricky").Verbose("Hello, world!");
9+
SelfLog.Enable(Console.Error);
3810

39-
logger.Information("Destructure with max object nesting depth:\n{@NestedObject}",
40-
new { FiveDeep = new { Two = new { Three = new { Four = new { Five = "the end" } } } } });
11+
Thread.CurrentThread.Name = "Main thread";
4112

42-
logger.Information("Destructure with max string length:\n{@LongString}",
43-
new { TwentyChars = "0123456789abcdefghij" });
13+
var configuration = new ConfigurationBuilder()
14+
.SetBasePath(Directory.GetCurrentDirectory())
15+
.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true)
16+
.Build();
4417

45-
logger.Information("Destructure with max collection count:\n{@BigData}",
46-
new { TenItems = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" } });
18+
var logger = new LoggerConfiguration()
19+
.ReadFrom.Configuration(configuration)
20+
.CreateLogger();
4721

48-
logger.Information("Destructure with policy to strip password:\n{@LoginData}",
49-
new LoginData { Username = "BGates", Password = "isityearoflinuxyet" });
22+
logger.Information("Args: {Args}", args);
5023

51-
Console.WriteLine("\nPress \"q\" to quit, or any other key to run again.\n");
52-
}
53-
while (!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q'));
54-
}
55-
}
56-
57-
// The filter syntax in the sample configuration file is
58-
// processed by the Serilog.Filters.Expressions package.
59-
public class CustomFilter : ILogEventFilter
24+
do
6025
{
61-
readonly LogEventLevel _levelFilter;
26+
logger.ForContext<Program>().Information("Hello, world!");
27+
logger.ForContext<Program>().Error("Hello, world!");
28+
logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Warning("Hello, world!");
29+
logger.ForContext(Constants.SourceContextPropertyName, "Microsoft").Error("Hello, world!");
30+
logger.ForContext(Constants.SourceContextPropertyName, "MyApp.Something.Tricky").Verbose("Hello, world!");
6231

63-
public CustomFilter(LogEventLevel levelFilter = LogEventLevel.Information)
64-
{
65-
_levelFilter = levelFilter;
66-
}
32+
logger.Information("Destructure with max object nesting depth:\n{@NestedObject}",
33+
new { FiveDeep = new { Two = new { Three = new { Four = new { Five = "the end" } } } } });
6734

68-
public bool IsEnabled(LogEvent logEvent)
69-
{
70-
return logEvent.Level >= _levelFilter;
71-
}
72-
}
35+
logger.Information("Destructure with max string length:\n{@LongString}",
36+
new { TwentyChars = "0123456789abcdefghij" });
7337

74-
public class LoginData
75-
{
76-
public string? Username;
77-
// ReSharper disable once NotAccessedField.Global
78-
public string? Password;
79-
}
80-
81-
public class CustomPolicy : IDestructuringPolicy
82-
{
83-
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue? result)
84-
{
85-
result = null;
38+
logger.Information("Destructure with max collection count:\n{@BigData}",
39+
new { TenItems = new[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" } });
8640

87-
if (value is LoginData loginData)
88-
{
89-
result = new StructureValue(
90-
new List<LogEventProperty>
91-
{
92-
new("Username", new ScalarValue(loginData.Username))
93-
});
94-
}
41+
logger.Information("Destructure with policy to strip password:\n{@LoginData}",
42+
new LoginData { Username = "BGates", Password = "isityearoflinuxyet" });
9543

96-
return (result != null);
97-
}
44+
Console.WriteLine("\nPress \"q\" to quit, or any other key to run again.\n");
9845
}
46+
while (!args.Contains("--run-once") && (Console.ReadKey().KeyChar != 'q'));

sample/Sample/Sample.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;netcoreapp3.1;net462</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;net462</TargetFrameworks>
55
<OutputType>Exe</OutputType>
66
</PropertyGroup>
77

@@ -14,14 +14,15 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
1818
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
1919
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
2020
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
2121
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
2222
<PackageReference Include="Serilog.Expressions" Version="3.3.0" />
2323
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
2424
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
25+
<PackageReference Include="PolySharp" Version="1.13.1" PrivateAssets="all" />
2526
</ItemGroup>
2627

2728
<ItemGroup>

src/Serilog.Settings.Configuration/ConfigurationLoggerConfigurationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,20 @@ public static LoggerConfiguration Configuration(
228228
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, DependencyContext? dependencyContext)
229229
{
230230
var assemblyFinder = dependencyContext == null ? AssemblyFinder.Auto() : AssemblyFinder.ForDependencyContext(dependencyContext);
231-
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
231+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName!);
232232
return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration);
233233
}
234234

235235
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, ConfigurationAssemblySource source)
236236
{
237237
var assemblyFinder = AssemblyFinder.ForSource(source);
238-
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
238+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName!);
239239
return new ConfigurationReader(section, assemblyFinder, readerOptions, configuration);
240240
}
241241

242242
static ConfigurationReader GetConfigurationReader(IConfiguration configuration, ConfigurationReaderOptions readerOptions, IReadOnlyCollection<Assembly> assemblies)
243243
{
244-
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName);
244+
var section = string.IsNullOrWhiteSpace(readerOptions.SectionName) ? configuration : configuration.GetSection(readerOptions.SectionName!);
245245
return new ConfigurationReader(section, assemblies, new ResolutionContext(configuration, readerOptions));
246246
}
247247
}

src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
<PropertyGroup>
44
<Description>Microsoft.Extensions.Configuration (appsettings.json) support for Serilog.</Description>
5-
<VersionPrefix>4.0.0</VersionPrefix>
5+
<!-- This must match the major and minor components of the referenced Microsoft.Extensions.Logging package. -->
6+
<VersionPrefix>7.0.0</VersionPrefix>
67
<Authors>Serilog Contributors</Authors>
7-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
8+
<!-- These must match the Dependencies tab in https://www.nuget.org/packages/microsoft.settings.configuration at
9+
the target version. -->
10+
<TargetFrameworks>net462;netstandard2.0;net6.0;net7.0</TargetFrameworks>
811
<GenerateDocumentationFile>true</GenerateDocumentationFile>
912
<AssemblyName>Serilog.Settings.Configuration</AssemblyName>
1013
<PackageTags>serilog;json</PackageTags>
@@ -23,11 +26,14 @@
2326

2427
<ItemGroup>
2528
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
26-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
27-
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
28-
<PackageReference Include="PolySharp" Version="1.12.1" PrivateAssets="All" />
29-
<PackageReference Include="Serilog" Version="2.10.0" />
29+
<PackageReference Include="PolySharp" Version="1.13.1" PrivateAssets="All" />
30+
<PackageReference Include="Serilog" Version="2.12.0" />
3031
<None Include="..\..\assets\icon.png" Pack="true" PackagePath="" Visible="false" />
3132
</ItemGroup>
3233

34+
<ItemGroup>
35+
<!-- The versions of all references in this group must match the major and minor components of the package version prefix. -->
36+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
37+
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
38+
</ItemGroup>
3339
</Project>

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void ProcessLevelSwitchDeclarations()
129129
}
130130
else
131131
{
132-
var initialLevel = ParseLogEventLevel(switchInitialLevel);
132+
var initialLevel = ParseLogEventLevel(switchInitialLevel!);
133133
newSwitch = new LoggingLevelSwitch(initialLevel);
134134
}
135135

@@ -171,17 +171,17 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
171171
_resolutionContext.ReaderOptions.OnLevelSwitchCreated?.Invoke(overridePrefix, levelSwitch);
172172
});
173173
}
174-
else
174+
else if (!string.IsNullOrEmpty(overridenLevelOrSwitch))
175175
{
176-
var overrideSwitch = _resolutionContext.LookUpLevelSwitchByName(overridenLevelOrSwitch);
176+
var overrideSwitch = _resolutionContext.LookUpLevelSwitchByName(overridenLevelOrSwitch!);
177177
// not calling ApplyMinimumLevel local function because here we have a reference to a LogLevelSwitch already
178178
loggerConfiguration.MinimumLevel.Override(overridePrefix, overrideSwitch);
179179
}
180180
}
181181

182182
void ApplyMinimumLevelConfiguration(IConfigurationSection directive, Action<LoggerMinimumLevelConfiguration, LoggingLevelSwitch> applyConfigAction)
183183
{
184-
var minimumLevel = ParseLogEventLevel(directive.Value);
184+
var minimumLevel = ParseLogEventLevel(directive.Value!);
185185

186186
var levelSwitch = new LoggingLevelSwitch(minimumLevel);
187187
applyConfigAction(loggerConfiguration.MinimumLevel, levelSwitch);
@@ -293,7 +293,8 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
293293
{
294294
foreach (var enrichPropertyDirective in propertiesDirective.GetChildren())
295295
{
296-
loggerConfiguration.Enrich.WithProperty(enrichPropertyDirective.Key, enrichPropertyDirective.Value);
296+
// Null is an acceptable value here; annotations on Serilog need updating.
297+
loggerConfiguration.Enrich.WithProperty(enrichPropertyDirective.Key, enrichPropertyDirective.Value!);
297298
}
298299
}
299300
}
@@ -359,7 +360,7 @@ internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSecti
359360
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration section, AssemblyFinder assemblyFinder)
360361
{
361362
var serilogAssembly = typeof(ILogger).Assembly;
362-
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName] = serilogAssembly };
363+
var assemblies = new Dictionary<string, Assembly> { [serilogAssembly.FullName!] = serilogAssembly };
363364

364365
var usingSection = section.GetSection("Using");
365366
if (usingSection.GetChildren().Any())
@@ -371,16 +372,16 @@ static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration
371372
"A zero-length or whitespace assembly name was supplied to a Serilog.Using configuration statement.");
372373

373374
var assembly = Assembly.Load(new AssemblyName(simpleName));
374-
if (!assemblies.ContainsKey(assembly.FullName))
375-
assemblies.Add(assembly.FullName, assembly);
375+
if (!assemblies.ContainsKey(assembly.FullName!))
376+
assemblies.Add(assembly.FullName!, assembly);
376377
}
377378
}
378379

379380
foreach (var assemblyName in assemblyFinder.FindAssembliesContainingName("serilog"))
380381
{
381382
var assumed = Assembly.Load(assemblyName);
382-
if (assumed != null && !assemblies.ContainsKey(assumed.FullName))
383-
assemblies.Add(assumed.FullName, assumed);
383+
if (assumed != null && !assemblies.ContainsKey(assumed.FullName!))
384+
assemblies.Add(assumed.FullName!, assumed);
384385
}
385386

386387
return assemblies.Values.ToList().AsReadOnly();

src/Serilog.Settings.Configuration/Settings/Configuration/ObjectArgumentValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ internal static bool TryBuildCtorExpression(
115115

116116
var type = typeDirective switch
117117
{
118-
not null => Type.GetType(section.GetValue<string>(typeDirective), throwOnError: false),
118+
not null => Type.GetType(section.GetValue<string>(typeDirective)!, throwOnError: false),
119119
null => parameterType,
120120
};
121121

test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ public void MethodsAreSelectedBasedOnCountOfMatchedArgumentsAndThenStringType()
189189
{
190190
new object[] { GetConfigRoot(appsettingsJsonLevel: minimumLevelFlatTemplate.Format(LogEventLevel.Error)), LogEventLevel.Error },
191191
new object[] { GetConfigRoot(appsettingsDevelopmentJsonLevel: minimumLevelFlatTemplate.Format(LogEventLevel.Error)), LogEventLevel.Error },
192-
new object[] { GetConfigRoot(envVariables: new Dictionary<string, string>() {{minimumLevelFlatKey, LogEventLevel.Error.ToString()}}), LogEventLevel.Error},
192+
new object[] { GetConfigRoot(envVariables: new Dictionary<string, string?> {{minimumLevelFlatKey, LogEventLevel.Error.ToString()}}), LogEventLevel.Error},
193193
new object[] { GetConfigRoot(
194194
appsettingsJsonLevel: minimumLevelFlatTemplate.Format(LogEventLevel.Debug),
195-
envVariables: new Dictionary<string, string>() {{minimumLevelFlatKey, LogEventLevel.Error.ToString()}}),
195+
envVariables: new Dictionary<string, string?> {{minimumLevelFlatKey, LogEventLevel.Error.ToString()}}),
196196
LogEventLevel.Error
197197
}
198198
};
@@ -214,7 +214,7 @@ public void FlatMinimumLevelCorrectOneIsEnabledOnLogger(IConfigurationRoot root,
214214
new object[] { GetConfigRoot(appsettingsJsonLevel: minimumLevelObjectTemplate.Format(LogEventLevel.Error)), LogEventLevel.Error },
215215
new object[] { GetConfigRoot(appsettingsJsonLevel: minimumLevelObjectTemplate.Format(LogEventLevel.Error.ToString().ToUpper())), LogEventLevel.Error },
216216
new object[] { GetConfigRoot(appsettingsDevelopmentJsonLevel: minimumLevelObjectTemplate.Format(LogEventLevel.Error)), LogEventLevel.Error },
217-
new object[] { GetConfigRoot(envVariables: new Dictionary<string, string>(){{minimumLevelObjectKey, LogEventLevel.Error.ToString() } }), LogEventLevel.Error },
217+
new object[] { GetConfigRoot(envVariables: new Dictionary<string, string?>{{minimumLevelObjectKey, LogEventLevel.Error.ToString() } }), LogEventLevel.Error },
218218
new object[] { GetConfigRoot(
219219
appsettingsJsonLevel: minimumLevelObjectTemplate.Format(LogEventLevel.Error),
220220
appsettingsDevelopmentJsonLevel: minimumLevelObjectTemplate.Format(LogEventLevel.Debug)),
@@ -254,7 +254,7 @@ public void ObjectMinimumLevelCorrectOneIsEnabledOnLogger(IConfigurationRoot roo
254254
new object[]
255255
{
256256
GetConfigRoot(
257-
envVariables: new Dictionary<string, string>()
257+
envVariables: new Dictionary<string, string?>()
258258
{
259259
{minimumLevelObjectKey, LogEventLevel.Error.ToString()},
260260
{minimumLevelFlatKey, LogEventLevel.Debug.ToString()}

test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net48</TargetFrameworks>
5-
<TargetFrameworks>$(TargetFrameworks);net7.0;netcoreapp3.1</TargetFrameworks>
5+
<TargetFrameworks>$(TargetFrameworks);net7.0</TargetFrameworks>
66
</PropertyGroup>
77

88
<ItemGroup>
@@ -17,7 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
2121
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
2222
<PackageReference Include="Serilog.Expressions" Version="3.3.0" />
2323
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />

0 commit comments

Comments
 (0)