diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..acd3bc6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +# If this file is renamed, the incrementing run attempt number will be reset. + +name: CI + +on: + push: + branches: [ "dev", "main" ] + pull_request: + branches: [ "dev", "main" ] + +env: + CI_BUILD_NUMBER_BASE: ${{ github.run_number }} + CI_TARGET_BRANCH: ${{ github.head_ref || github.ref_name }} + +jobs: + build: + + # The build must run on Windows so that .NET Framework targets can be built and tested. + runs-on: windows-latest + + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + - name: Setup + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 9.0.x + - name: Compute build number + shell: bash + run: | + echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+2300))" >> $GITHUB_ENV + - name: Build and Publish + env: + DOTNET_CLI_TELEMETRY_OPTOUT: true + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: pwsh + run: | + ./Build.ps1 diff --git a/.gitignore b/.gitignore index 7b17e06..febeee0 100644 --- a/.gitignore +++ b/.gitignore @@ -200,4 +200,7 @@ project.lock.json # JetBrains Rider .idea -.vscode \ No newline at end of file +.vscode + +.DS_Store + diff --git a/Build.ps1 b/Build.ps1 index 431f4e2..e798284 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -1,58 +1,79 @@ -echo "build: Build started" +Write-Output "build: Tool versions follow" -$env:Path = "$pwd/.dotnetcli;$env:Path" +dotnet --version +dotnet --list-sdks + +Write-Output "build: Build started" Push-Location $PSScriptRoot +try { + if(Test-Path .\artifacts) { + Write-Output "build: Cleaning ./artifacts" + Remove-Item ./artifacts -Force -Recurse + } -if(Test-Path .\artifacts) { - echo "build: Cleaning .\artifacts" - Remove-Item .\artifacts -Force -Recurse -} + & dotnet restore --no-cache -& dotnet restore --no-cache + $dbp = [Xml] (Get-Content .\Directory.Version.props) + $versionPrefix = $dbp.Project.PropertyGroup.VersionPrefix -$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL]; -$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL]; -$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"] + Write-Output "build: Package version prefix is $versionPrefix" -echo "build: Version suffix is $suffix" + $branch = @{ $true = $env:CI_TARGET_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$NULL -ne $env:CI_TARGET_BRANCH]; + $revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:CI_BUILD_NUMBER, 10); $false = "local" }[$NULL -ne $env:CI_BUILD_NUMBER]; + $suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)) -replace '([^a-zA-Z0-9\-]*)', '')-$revision"}[$branch -eq "main" -and $revision -ne "local"] + $commitHash = $(git rev-parse --short HEAD) + $buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""] -foreach ($src in ls src/*) { - Push-Location $src + Write-Output "build: Package version suffix is $suffix" + Write-Output "build: Build version suffix is $buildSuffix" - echo "build: Packaging project in $src" + & dotnet build -c Release --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true + if($LASTEXITCODE -ne 0) { throw "Build failed" } - if($suffix) { - & dotnet pack -c Release --include-source -o ..\..\artifacts --version-suffix=$suffix - } else { - & dotnet pack -c Release --include-source -o ..\..\artifacts - } + foreach ($src in Get-ChildItem src/*) { + Push-Location $src - if($LASTEXITCODE -ne 0) { throw "build failed" } + Write-Output "build: Packaging project in $src" - Pop-Location -} + if ($suffix) { + & dotnet pack -c Release --no-build --no-restore -o ../../artifacts --version-suffix=$suffix + } else { + & dotnet pack -c Release --no-build --no-restore -o ../../artifacts + } + if($LASTEXITCODE -ne 0) { throw "Packaging failed" } + + Pop-Location + } -foreach ($test in ls test/*.PerformanceTests) { - Push-Location $test + foreach ($test in Get-ChildItem test/*.Tests) { + Push-Location $test - echo "build: Building performance test project in $test" + Write-Output "build: Testing project in $test" - & dotnet build -c Release - if($LASTEXITCODE -ne 0) { throw "test failed" } + & dotnet test -c Release --no-build --no-restore + if($LASTEXITCODE -ne 0) { throw "Testing failed" } - Pop-Location -} + Pop-Location + } -foreach ($test in ls test/*.Tests) { - Push-Location $test + if ($env:NUGET_API_KEY) { + # GitHub Actions will only supply this to branch builds and not PRs. We publish + # builds from any branch this action targets (i.e. main and dev). - echo "build: Testing project in $test" + Write-Output "build: Publishing NuGet packages" - & dotnet test -c Release - if($LASTEXITCODE -ne 0) { throw "test failed" } + foreach ($nupkg in Get-ChildItem artifacts/*.nupkg) { + & dotnet nuget push -k $env:NUGET_API_KEY -s https://api.nuget.org/v3/index.json "$nupkg" + if($LASTEXITCODE -ne 0) { throw "Publishing failed" } + } + if (!($suffix)) { + Write-Output "build: Creating release for version $versionPrefix" + + iex "gh release create v$versionPrefix --title v$versionPrefix --generate-notes $(get-item ./artifacts/*.nupkg) $(get-item ./artifacts/*.snupkg)" + } + } +} finally { Pop-Location } - -Pop-Location diff --git a/Directory.Build.props b/Directory.Build.props index 2b620bc..c114992 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,12 +1,25 @@ - + + latest True - true + + true $(MSBuildThisFileDirectory)assets/Serilog.snk - true + false enable + enable + true + true + true + true + snupkg - + + + + + diff --git a/Directory.Build.targets b/Directory.Build.targets deleted file mode 100644 index faf2349..0000000 --- a/Directory.Build.targets +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/Directory.Version.props b/Directory.Version.props new file mode 100644 index 0000000..3077571 --- /dev/null +++ b/Directory.Version.props @@ -0,0 +1,6 @@ + + + + 9.0.0 + + diff --git a/Setup.ps1 b/Setup.ps1 deleted file mode 100644 index aa90b5b..0000000 --- a/Setup.ps1 +++ /dev/null @@ -1,9 +0,0 @@ -$ErrorActionPreference = "Stop" - -$RequiredDotnetVersion = $(cat ./global.json | convertfrom-json).sdk.version - -New-Item -ItemType Directory -Force "./build/" | Out-Null - -Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./build/installcli.ps1" -& ./build/installcli.ps1 -InstallDir "$pwd/.dotnetcli" -NoPath -Version $RequiredDotnetVersion -if ($LASTEXITCODE) { throw ".NET install failed" } diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index b7e0f44..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,26 +0,0 @@ -version: '{build}' -skip_tags: true -image: Visual Studio 2022 -install: -- pwsh: ./Setup.ps1 -- pwsh: mkdir -Force ".\build\" | Out-Null -build_script: -- pwsh: ./Build.ps1 -test: off -artifacts: -- path: artifacts/Serilog.*.nupkg -deploy: -- provider: NuGet - api_key: - secure: H96ajkMxwIafhF2vrr+UAUS10bFcAL/1wc3iphidRiYi9WoTc2i8shTLtF+75ODb - skip_symbols: true - on: - branch: /^(main|dev)$/ -- provider: GitHub - auth_token: - secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX - artifact: /Serilog.*\.nupkg/ - tag: v$(appveyor_build_version) - on: - branch: main - diff --git a/build.sh b/build.sh deleted file mode 100644 index 6d1ff38..0000000 --- a/build.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -dotnet --info -dotnet restore - -for path in src/**/*.csproj; do - dotnet build -f netstandard2.0 -c Release ${path} - -done - -for path in test/*.Tests/*.csproj; do - dotnet test -f netcoreapp2.0 -c Release ${path} -done \ No newline at end of file diff --git a/global.json b/global.json index 5ce8495..db8627a 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,7 @@ { "sdk": { - "version": "8.0.100" + "version": "9.0.100", + "allowPrerelease": false, + "rollForward": "latestFeature" } } diff --git a/samples/Sample/Program.cs b/samples/Sample/Program.cs index d70b9aa..b369510 100644 --- a/samples/Sample/Program.cs +++ b/samples/Sample/Program.cs @@ -3,33 +3,16 @@ using Serilog; using Serilog.Extensions.Logging; -// Creating a `LoggerProviderCollection` lets Serilog optionally write -// events through other dynamically-added MEL ILoggerProviders. -var providers = new LoggerProviderCollection(); - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Debug() .WriteTo.Console() - .WriteTo.Providers(providers) .CreateLogger(); var services = new ServiceCollection(); -services.AddSingleton(providers); -services.AddSingleton(sc => -{ - var providerCollection = sc.GetService(); - var factory = new SerilogLoggerFactory(null, true, providerCollection); - - foreach (var provider in sc.GetServices()) - factory.AddProvider(provider); - - return factory; -}); - -services.AddLogging(l => l.AddConsole()); +services.AddLogging(); +services.AddSingleton(new SerilogLoggerFactory()); -var serviceProvider = services.BuildServiceProvider(); +using var serviceProvider = services.BuildServiceProvider(); var logger = serviceProvider.GetRequiredService>(); var startTime = DateTimeOffset.UtcNow; @@ -65,4 +48,3 @@ logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); -serviceProvider.Dispose(); diff --git a/samples/Sample/Properties/launchSettings.json b/samples/Sample/Properties/launchSettings.json deleted file mode 100644 index 43e0c99..0000000 --- a/samples/Sample/Properties/launchSettings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "profiles": {} -} \ No newline at end of file diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index 951b742..793b13a 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -1,10 +1,8 @@  - net8.0 - Sample + net9.0 Exe - enable @@ -12,9 +10,9 @@ - - - + + + diff --git a/samples/SampleWithExternalScope/Program.cs b/samples/SampleWithExternalScope/Program.cs index 3a1a018..2b0aad7 100644 --- a/samples/SampleWithExternalScope/Program.cs +++ b/samples/SampleWithExternalScope/Program.cs @@ -30,14 +30,14 @@ // Add an ActivityListener (required, otherwise Activities don't actually get created if nothing is listening to them) ActivitySource.AddActivityListener(new ActivityListener { - ShouldListenTo = source => true, - Sample = (ref ActivityCreationOptions options) => ActivitySamplingResult.AllDataAndRecorded + ShouldListenTo = _ => true, + Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded }); // Run our test var activitySource = new ActivitySource("SomeActivitySource"); -var serviceProvider = services.BuildServiceProvider(); +using var serviceProvider = services.BuildServiceProvider(); var logger = serviceProvider.GetRequiredService>(); using var activity = activitySource.StartActivity(); @@ -53,4 +53,3 @@ logger.LogInformation("Hello world!"); -serviceProvider.Dispose(); diff --git a/samples/SampleWithExternalScope/SampleWithExternalScope.csproj b/samples/SampleWithExternalScope/SampleWithExternalScope.csproj index 045c31a..84cb0ac 100644 --- a/samples/SampleWithExternalScope/SampleWithExternalScope.csproj +++ b/samples/SampleWithExternalScope/SampleWithExternalScope.csproj @@ -2,10 +2,7 @@ Exe - net8.0 - enable - Exe - enable + net9.0 @@ -13,9 +10,9 @@ - - - + + + diff --git a/samples/SampleWithMelProviders/Program.cs b/samples/SampleWithMelProviders/Program.cs new file mode 100644 index 0000000..c8b88b1 --- /dev/null +++ b/samples/SampleWithMelProviders/Program.cs @@ -0,0 +1,69 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Extensions.Logging; + +// Creating a `LoggerProviderCollection` lets Serilog optionally write +// events through other dynamically-added MEL ILoggerProviders. +var providers = new LoggerProviderCollection(); + +// The sample sets up Serilog's console sink here: +Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.Console() + .WriteTo.Providers(providers) + .CreateLogger(); + +var services = new ServiceCollection(); + +services.AddSingleton(providers); +services.AddSingleton(sc => +{ + var providerCollection = sc.GetService(); + var factory = new SerilogLoggerFactory(null, true, providerCollection); + + foreach (var provider in sc.GetServices()) + factory.AddProvider(provider); + + return factory; +}); + +// ..and MEL's console provider here: +services.AddLogging(l => l.AddConsole()); + +using var serviceProvider = services.BuildServiceProvider(); +var logger = serviceProvider.GetRequiredService>(); + +var startTime = DateTimeOffset.UtcNow; +logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42); + +try +{ + throw new Exception("Boom!"); +} +catch (Exception ex) +{ + logger.LogCritical(ex, "Unexpected critical error starting application"); + logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null!); + // This write should not log anything + logger.Log(LogLevel.Critical, 0, null!, null, null!); + logger.LogError(ex, "Unexpected error"); + logger.LogWarning(ex, "Unexpected warning"); +} + +using (logger.BeginScope("Main")) +{ + logger.LogInformation("Waiting for user input"); + var key = Console.Read(); + logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key }); +} + +var endTime = DateTimeOffset.UtcNow; +logger.LogInformation(2, "Stopping at {StopTime}", endTime); + +logger.LogInformation("Stopping"); + +logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)"); +logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------"); +logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds); + diff --git a/samples/SampleWithMelProviders/SampleWithMelProviders.csproj b/samples/SampleWithMelProviders/SampleWithMelProviders.csproj new file mode 100644 index 0000000..793b13a --- /dev/null +++ b/samples/SampleWithMelProviders/SampleWithMelProviders.csproj @@ -0,0 +1,18 @@ + + + + net9.0 + Exe + + + + + + + + + + + + + diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index d0d5cb9..baca24a 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -18,21 +18,20 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9DF-AEDD-4AA6-BEA4-912DEF3E5B8E}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig - appveyor.yml = appveyor.yml Build.ps1 = Build.ps1 Directory.Build.props = Directory.Build.props - Directory.Build.targets = Directory.Build.targets README.md = README.md assets\Serilog.snk = assets\Serilog.snk - build.sh = build.sh - Setup.ps1 = Setup.ps1 global.json = global.json + Directory.Version.props = Directory.Version.props EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Benchmarks", "test\Serilog.Extensions.Logging.Benchmarks\Serilog.Extensions.Logging.Benchmarks.csproj", "{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWithExternalScope", "samples\SampleWithExternalScope\SampleWithExternalScope.csproj", "{653092A8-CBAD-40AA-A4CE-F8B19D6492C2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWithMelProviders", "samples\SampleWithMelProviders\SampleWithMelProviders.csproj", "{B1454759-126F-4F33-84EE-C8E19541DF79}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -59,6 +58,10 @@ Global {653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Debug|Any CPU.Build.0 = Debug|Any CPU {653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Release|Any CPU.ActiveCfg = Release|Any CPU {653092A8-CBAD-40AA-A4CE-F8B19D6492C2}.Release|Any CPU.Build.0 = Release|Any CPU + {B1454759-126F-4F33-84EE-C8E19541DF79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B1454759-126F-4F33-84EE-C8E19541DF79}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B1454759-126F-4F33-84EE-C8E19541DF79}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B1454759-126F-4F33-84EE-C8E19541DF79}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -69,6 +72,7 @@ Global {65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7} {6D5986FF-EECD-4E75-8BC6-A5F78AB549B2} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1} {653092A8-CBAD-40AA-A4CE-F8B19D6492C2} = {F2407211-6043-439C-8E06-3641634332E7} + {B1454759-126F-4F33-84EE-C8E19541DF79} = {F2407211-6043-439C-8E06-3641634332E7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10} diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs index 8966ef7..01b87f6 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs @@ -12,14 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. - using Serilog.Events; using Serilog.Parsing; using System.Collections; namespace Serilog.Extensions.Logging; -class CachingMessageTemplateParser +sealed class CachingMessageTemplateParser { readonly MessageTemplateParser _innerParser = new(); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs index d2159f3..f7ce438 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs @@ -16,9 +16,9 @@ namespace Serilog.Extensions.Logging; using System.Collections.Concurrent; using Microsoft.Extensions.Logging; -using Serilog.Events; +using Events; -class EventIdPropertyCache +sealed class EventIdPropertyCache { readonly int _maxCachedProperties; readonly ConcurrentDictionary _propertyCache = new(); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollection.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollection.cs index b944040..8799d7b 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollection.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollection.cs @@ -20,9 +20,9 @@ namespace Serilog.Extensions.Logging; /// /// A dynamically-modifiable collection of s. /// -public class LoggerProviderCollection : IDisposable +public sealed class LoggerProviderCollection : IDisposable { - volatile ILoggerProvider[] _providers = Array.Empty(); + volatile ILoggerProvider[] _providers = []; /// /// Add to the collection. @@ -37,7 +37,7 @@ public void AddProvider(ILoggerProvider provider) do { existing = _providers; - added = existing.Concat(new[] { provider }).ToArray(); + added = [..existing, provider]; } #pragma warning disable 420 // ref to a volatile field while (Interlocked.CompareExchange(ref _providers, added, existing) != existing); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.cs index 0febb52..5609905 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/LoggerProviderCollectionSink.cs @@ -18,7 +18,7 @@ namespace Serilog.Extensions.Logging; -class LoggerProviderCollectionSink : ILogEventSink, IDisposable +sealed class LoggerProviderCollectionSink : ILogEventSink, IDisposable { readonly LoggerProviderCollection _providers; diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs index 20253f8..287608b 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs @@ -44,10 +44,7 @@ public SerilogLogValues(MessageTemplate messageTemplate, IReadOnlyDictionary("{OriginalFormat}", _messageTemplate.Text); } - public KeyValuePair this[int index] - { - get => _values[index]; - } + public KeyValuePair this[int index] => _values[index]; public int Count => _properties.Count + 1; diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs index a21d631..05f6bdd 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs @@ -12,7 +12,7 @@ namespace Serilog.Extensions.Logging; -class SerilogLogger : FrameworkLogger +sealed class SerilogLogger : FrameworkLogger { internal static readonly ConcurrentDictionary DestructureDictionary = new(); internal static readonly ConcurrentDictionary StringifyDictionary = new(); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs index b85dbcb..12ece4c 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerFactory.cs @@ -20,7 +20,7 @@ namespace Serilog.Extensions.Logging; /// /// A complete Serilog-backed implementation of the .NET Core logging infrastructure. /// -public class SerilogLoggerFactory : ILoggerFactory +public sealed class SerilogLoggerFactory : ILoggerFactory { readonly LoggerProviderCollection? _providerCollection; readonly SerilogLoggerProvider _provider; diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs index d80e4e9..d6b18d1 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Serilog.Core; using Serilog.Events; @@ -14,7 +13,7 @@ namespace Serilog.Extensions.Logging; /// An that pipes events through Serilog. /// [ProviderAlias("Serilog")] -public class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher, ISupportExternalScope +public sealed class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher, ISupportExternalScope #if FEATURE_ASYNCDISPOSABLE , IAsyncDisposable #endif @@ -28,7 +27,7 @@ public class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher, ISuppor #if FEATURE_ASYNCDISPOSABLE readonly Func? _disposeAsync; #endif - private IExternalScopeProvider? _externalScopeProvider; + IExternalScopeProvider? _externalScopeProvider; /// /// Construct a . @@ -38,7 +37,7 @@ public class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher, ISuppor public SerilogLoggerProvider(ILogger? logger = null, bool dispose = false) { if (logger != null) - _logger = logger.ForContext(new[] { this }); + _logger = logger.ForContext([this]); if (dispose) { @@ -46,12 +45,16 @@ public SerilogLoggerProvider(ILogger? logger = null, bool dispose = false) { _dispose = () => (logger as IDisposable)?.Dispose(); #if FEATURE_ASYNCDISPOSABLE - _disposeAsync = () => + _disposeAsync = async () => { - // Dispose via IAsyncDisposable if possible, otherwise fall back to IDisposable - if (logger is IAsyncDisposable asyncDisposable) return asyncDisposable.DisposeAsync(); - else (logger as IDisposable)?.Dispose(); - return default; + if (logger is IAsyncDisposable asyncDisposable) + { + await asyncDisposable.DisposeAsync(); + } + else + { + (logger as IDisposable)?.Dispose(); + } }; #endif } @@ -94,7 +97,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) if (scopeItem != null) { - scopeItems ??= new List(); + scopeItems ??= []; scopeItems.Add(scopeItem); } } diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs index ca16691..8e71133 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs @@ -6,7 +6,7 @@ namespace Serilog.Extensions.Logging; -class SerilogLoggerScope : IDisposable +sealed class SerilogLoggerScope : IDisposable { const string NoName = "None"; diff --git a/src/Serilog.Extensions.Logging/Extensions/StringExtensions.cs b/src/Serilog.Extensions.Logging/Extensions/StringExtensions.cs index 0e072b9..ac772fd 100644 --- a/src/Serilog.Extensions.Logging/Extensions/StringExtensions.cs +++ b/src/Serilog.Extensions.Logging/Extensions/StringExtensions.cs @@ -1,8 +1,8 @@ -using System.Runtime.CompilerServices; +#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER +using System.Runtime.CompilerServices; namespace Serilog.Extensions; -#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER static class StringExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs b/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs index 50afb45..468e64f 100644 --- a/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs +++ b/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs @@ -6,8 +6,6 @@ using System.Reflection; using System.Runtime.CompilerServices; -[assembly: AssemblyVersion("7.0.0.0")] - [assembly: InternalsVisibleTo("Serilog.Extensions.Logging.Tests, PublicKey=" + "0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" + "6fe0fe83ef33c1080bf30690765bc6eb0df26ebfdf8f21670c64265b30db09f73a0dea5b3db4c9" + diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index d1a4cef..e1a3a12 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -2,24 +2,16 @@ Low-level Serilog provider for Microsoft.Extensions.Logging - - 8.0.1 Microsoft;Serilog Contributors - net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0 + net462;netstandard2.0;netstandard2.1;net8.0;net9.0 true serilog;Microsoft.Extensions.Logging serilog-extension-nuget.png https://github.com/serilog/serilog-extensions-logging Apache-2.0 - false Serilog - git - embedded - true - true - True README.md NU5118 @@ -28,26 +20,21 @@ - - + - + - $(DefineConstants);FEATURE_ITUPLE;FEATURE_ASYNCDISPOSABLE - - - - $(DefineConstants);FEATURE_ITUPLE;FEATURE_ASYNCDISPOSABLE + $(DefineConstants);FEATURE_ITUPLE - + $(DefineConstants);FEATURE_ITUPLE;FEATURE_ASYNCDISPOSABLE - + $(DefineConstants);FEATURE_ITUPLE;FEATURE_ASYNCDISPOSABLE diff --git a/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs index 1757de0..e7fe105 100644 --- a/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs +++ b/src/Serilog.Extensions.Logging/SerilogLoggingBuilderExtensions.cs @@ -38,7 +38,7 @@ public static ILoggingBuilder AddSerilog(this ILoggingBuilder builder, ILogger? if (dispose) { - builder.Services.AddSingleton(services => new SerilogLoggerProvider(logger, true)); + builder.Services.AddSingleton(_ => new SerilogLoggerProvider(logger, true)); } else { diff --git a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj index 155847e..34aa874 100644 --- a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj +++ b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj @@ -1,8 +1,8 @@ - net8.0 - enable + net8.0;net9.0 + false @@ -10,10 +10,10 @@ - - - - + + + + diff --git a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj index 7c1a2f8..aace2e4 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -1,8 +1,8 @@ - net8.0;net48 - enable + net8.0;net9.0;net48 + false @@ -14,11 +14,11 @@ - - - + + + - +