diff --git a/.gitignore b/.gitignore index c90bd95..db9e81a 100644 --- a/.gitignore +++ b/.gitignore @@ -201,3 +201,4 @@ FakesAssemblies/ project.lock.json artifacts/ +/test/TestApp-* diff --git a/Build.ps1 b/Build.ps1 index 1513382..18d5bfb 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -30,9 +30,6 @@ if($LASTEXITCODE -ne 0) { throw 'pack failed' } Write-Output "build: Testing" -# Dotnet test doesn't run separate TargetFrameworks in parallel: https://github.com/dotnet/sdk/issues/19147 -# Workaround: use `dotnet test` on dlls directly in order to pass the `--parallel` option to vstest. -# The _reported_ runtime is wrong but the _actual_ used runtime is correct, see https://github.com/microsoft/vstest/issues/2037#issuecomment-720549173 -& dotnet test test\Serilog.Settings.Configuration.Tests\bin\Release\*\Serilog.Settings.Configuration.Tests.dll --parallel +& dotnet test test\Serilog.Settings.Configuration.Tests\Serilog.Settings.Configuration.Tests.csproj -if($LASTEXITCODE -ne 0) { throw 'unit tests failed' } \ No newline at end of file +if($LASTEXITCODE -ne 0) { throw 'unit tests failed' } diff --git a/README.md b/README.md index 9695b7b..b0efd41 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,74 @@ Some Serilog packages require a reference to a logger configuration object. The }, ``` +### Destructuring + +Destructuring means extracting pieces of information from an object and create properties with values; Serilog offers the `@` [structure-capturing operator](https://github.com/serilog/serilog/wiki/Structured-Data#preserving-object-structure). In case there is a need to customize the way log events are serialized (e.g., hide property values or replace them with something else), one can define several destructuring policies, like this: + +```yaml +"Destructure": [ + { + "Name": "With", + "Args": { + "policy": "MyFirstNamespace.FirstDestructuringPolicy, MyFirstAssembly" + } + }, + { + "Name": "With", + "Args": { + "policy": "policy": "MySecondNamespace.SecondDestructuringPolicy, MySecondAssembly" + } + }, + { + "Name": "With", + "Args": { + "policy": "policy": "MyThirdNamespace.ThirdDestructuringPolicy, MyThirdAssembly" + } + }, +], +``` + +This is how the first destructuring policy would look like: + +```csharp +namespace MyFirstNamespace; + +public record MyDto(int Id, int Name); + +public class FirstDestructuringPolicy : IDestructuringPolicy +{ + public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, + [NotNullWhen(true)] out LogEventPropertyValue? result) + { + if (value is not MyDto dto) + { + result = null; + return false; + } + + result = new StructureValue(new List + { + new LogEventProperty("Identifier", new ScalarValue(deleteTodoItemInfo.Id)), + new LogEventProperty("NormalizedName", new ScalarValue(dto.Name.ToUpperInvariant())) + }); + + return true; + } +} +``` + +Assuming Serilog needs to destructure an argument of type **MyDto** when handling a log event: + +```csharp +logger.Information("About to process input: {@MyDto} ...", myDto); +``` + +it will apply **FirstDestructuringPolicy** which will convert **MyDto** instance to a **StructureValue** instance; a Serilog console sink would write the following entry: + +```text +About to process input: {"Identifier": 191, "NormalizedName": "SOME_UPPER_CASE_NAME"} ... +``` + ## Arguments binding When the configuration specifies a discrete value for a parameter (such as a string literal), the package will attempt to convert that value to the target method's declared CLR type of the parameter. Additional explicit handling is provided for parsing strings to `Uri`, `TimeSpan`, `enum`, arrays and custom collections. diff --git a/appveyor.yml b/appveyor.yml index b804999..eb9d04e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -36,7 +36,7 @@ artifacts: deploy: - provider: NuGet api_key: - secure: 60gpLnipFCiKLpS7ECI1C6EPJW27KzVwqrBVkEzX6FIMTmsG//HD3p8Oq7WdQPm8 + secure: JIfNMRv3l/2dmM/i//mpeEKqgxyEcnGr8XFlEoSDgp2JDVmRP8nUxc4gYznBvXQV on: branch: /^(main|dev)$/ OS: Windows_NT diff --git a/global.json b/global.json index 1d927af..5ce8495 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,5 @@ { "sdk": { - "version": "7.0.201", - "allowPrerelease": false, - "rollForward": "latestFeature" + "version": "8.0.100" } } diff --git a/sample/Sample/Sample.csproj b/sample/Sample/Sample.csproj index 6357b2b..ce3fee4 100644 --- a/sample/Sample/Sample.csproj +++ b/sample/Sample/Sample.csproj @@ -1,7 +1,7 @@  - net6.0;net7.0;net462 + net462;net6.0;net7.0;net8.0 Exe @@ -14,15 +14,15 @@ - + - + - - - + + + - + diff --git a/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj b/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj index 60b1c67..8ccfbb3 100644 --- a/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj +++ b/src/Serilog.Settings.Configuration/Serilog.Settings.Configuration.csproj @@ -3,16 +3,19 @@ Microsoft.Extensions.Configuration (appsettings.json) support for Serilog. - 7.0.1 + 8.0.0 Serilog Contributors - net462;netstandard2.0;net6.0;net7.0 + net462;netstandard2.0;net6.0;net7.0;net8.0 true Serilog.Settings.Configuration serilog;json icon.png + README.md Apache-2.0 + https://github.com/serilog/serilog-settings-configuration + $(PackageProjectUrl)/releases Serilog true true @@ -26,14 +29,15 @@ - - + + + - - + + diff --git a/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs b/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs index 4916f35..d505ec1 100644 --- a/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs +++ b/test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs @@ -395,12 +395,12 @@ public void TestMinimumLevelOverridesForChildContext() log.Write(Some.DebugEvent()); Assert.Null(evt); - var custom = log.ForContext(Constants.SourceContextPropertyName, typeof(System.Threading.Tasks.Task).FullName + "<42>"); + var custom = log.ForContext(Constants.SourceContextPropertyName, typeof(Task).FullName + "<42>"); custom.Write(Some.DebugEvent()); Assert.NotNull(evt); evt = null; - var systemThreadingLogger = log.ForContext(); + var systemThreadingLogger = log.ForContext(); systemThreadingLogger.Write(Some.DebugEvent()); Assert.NotNull(evt); } diff --git a/test/Serilog.Settings.Configuration.Tests/DllScanningAssemblyFinderTests.cs b/test/Serilog.Settings.Configuration.Tests/DllScanningAssemblyFinderTests.cs index 120ac3c..495bd68 100644 --- a/test/Serilog.Settings.Configuration.Tests/DllScanningAssemblyFinderTests.cs +++ b/test/Serilog.Settings.Configuration.Tests/DllScanningAssemblyFinderTests.cs @@ -9,18 +9,11 @@ namespace Serilog.Settings.Configuration.Tests; public class DllScanningAssemblyFinderTests { +#if NETFRAMEWORK const string BinDir1 = "bin1"; const string BinDir2 = "bin2"; const string BinDir3 = "bin3"; - [Fact] - public void ShouldProbeCurrentDirectory() - { - var assemblyNames = new DllScanningAssemblyFinder().FindAssembliesContainingName("TestDummies"); - Assert.Single(assemblyNames); - } - -#if NETFRAMEWORK [Fact] public void ShouldProbePrivateBinPath() { @@ -61,4 +54,11 @@ static void DoTestInner() } } #endif + + [Fact] + public void ShouldProbeCurrentDirectory() + { + var assemblyNames = new DllScanningAssemblyFinder().FindAssembliesContainingName("TestDummies"); + Assert.Single(assemblyNames); + } } diff --git a/test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj b/test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj index f1915c1..b04410e 100644 --- a/test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj +++ b/test/Serilog.Settings.Configuration.Tests/Serilog.Settings.Configuration.Tests.csproj @@ -2,7 +2,7 @@ net48 - $(TargetFrameworks);net7.0;net6.0 + $(TargetFrameworks);net6.0;net7.0;net8.0 @@ -17,16 +17,16 @@ - - - - - - - - - - + + + + + + + + + + diff --git a/test/TestApp/TestApp.csproj b/test/TestApp/TestApp.csproj index e60f2b7..b814a4d 100644 --- a/test/TestApp/TestApp.csproj +++ b/test/TestApp/TestApp.csproj @@ -9,6 +9,7 @@ false none true + true @@ -21,9 +22,9 @@ - + - + diff --git a/test/TestDummies/DummyPolicy.cs b/test/TestDummies/DummyPolicy.cs index 2104abf..5774510 100644 --- a/test/TestDummies/DummyPolicy.cs +++ b/test/TestDummies/DummyPolicy.cs @@ -1,6 +1,7 @@ using Serilog.Core; using Serilog.Events; using System.Collections; +using System.Diagnostics.CodeAnalysis; namespace TestDummies; @@ -24,7 +25,7 @@ public class DummyPolicy : IDestructuringPolicy public decimal Decimal { get; set; } - public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue? result) + public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result) { result = null; return false; diff --git a/test/TestDummies/TestDummies.csproj b/test/TestDummies/TestDummies.csproj index e410bbb..c5be233 100644 --- a/test/TestDummies/TestDummies.csproj +++ b/test/TestDummies/TestDummies.csproj @@ -5,11 +5,12 @@ - + + - +