From d76a80f2c55b611cd0283ec7124c36f2e402f5f8 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 14:51:20 +1000 Subject: [PATCH 1/6] v7.0 - pin to MEL v7, including matching target frameworks; update to Serilog v2.12 and apply annotations for nullable reference types --- Directory.Build.props | 3 +- README.md | 16 +-- appveyor.yml | 4 +- samples/Sample/Program.cs | 107 ++++++++---------- samples/Sample/Sample.csproj | 5 +- serilog-extensions-logging.sln.DotSettings | 2 + .../Logging/CachingMessageTemplateParser.cs | 2 +- .../Extensions/Logging/LevelConvert.cs | 6 +- .../Extensions/Logging/SerilogLogValues.cs | 18 +-- .../Extensions/Logging/SerilogLogger.cs | 23 ++-- .../Logging/SerilogLoggerProvider.cs | 6 +- .../Extensions/Logging/SerilogLoggerScope.cs | 4 +- .../Properties/AssemblyInfo.cs | 9 +- .../Serilog.Extensions.Logging.csproj | 10 +- ...rilog.Extensions.Logging.Benchmarks.csproj | 8 +- .../Serilog.Extensions.Logging.Tests.csproj | 8 +- .../Support/DisposeTrackingLogger.cs | 97 ++++++++-------- .../Support/ExtensionsProvider.cs | 15 +-- 18 files changed, 170 insertions(+), 173 deletions(-) create mode 100644 serilog-extensions-logging.sln.DotSettings diff --git a/Directory.Build.props b/Directory.Build.props index 3883932..2b620bc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,7 +7,6 @@ $(MSBuildThisFileDirectory)assets/Serilog.snk true enable - enable - \ No newline at end of file + diff --git a/README.md b/README.md index 9649d58..4c67766 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 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/) +# 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/) A Serilog provider for [Microsoft.Extensions.Logging](https://www.nuget.org/packages/Microsoft.Extensions.Logging), the logging subsystem used by ASP.NET Core. @@ -16,9 +16,9 @@ The package implements `AddSerilog()` on `ILoggingBuilder` and `ILoggerFactory` **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. -```powershell -Install-Package Serilog.Extensions.Logging -DependencyVersion Highest -Install-Package Serilog.Sinks.Console +```sh +dotnet add package Serilog.Extensions.Logging +dotnet add package Serilog.Sinks.Console ``` **Next**, in your application's `Startup` method, configure Serilog first: @@ -34,7 +34,7 @@ public class Startup .Enrich.FromLogContext() .WriteTo.Console() .CreateLogger(); - + // Other startup code ``` @@ -46,7 +46,7 @@ call `AddSerilog()` on the provided `loggingBuilder`. { services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)); - + // Other services ... } ``` @@ -60,7 +60,7 @@ call `AddSerilog()` on the provided `loggingBuilder`. IApplicationLifetime appLifetime) { loggerfactory.AddSerilog(); - + // Ensure any buffered events are sent at shutdown appLifetime.ApplicationStopped.Register(Log.CloseAndFlush); ``` @@ -69,7 +69,7 @@ That's it! With the level bumped up a little you should see log output like: ``` [22:14:44.646 DBG] RouteCollection.RouteAsync - Routes: + Routes: Microsoft.AspNet.Mvc.Routing.AttributeRoute {controller=Home}/{action=Index}/{id?} Handled? True diff --git a/appveyor.yml b/appveyor.yml index e6e8a2f..3e4a484 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,12 +14,12 @@ deploy: secure: EN9f+XXE3fW+ebL4wxrIbafdtbNvRfddBN8UUixvctYh4qMBHzr1JdnM83QsM1zo skip_symbols: true on: - branch: /^(master|dev)$/ + branch: /^(main|dev)$/ - provider: GitHub auth_token: secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX artifact: /Serilog.*\.nupkg/ tag: v$(appveyor_build_version) on: - branch: master + branch: main diff --git a/samples/Sample/Program.cs b/samples/Sample/Program.cs index 9ba6bf0..d70b9aa 100644 --- a/samples/Sample/Program.cs +++ b/samples/Sample/Program.cs @@ -3,75 +3,66 @@ using Serilog; using Serilog.Extensions.Logging; -namespace Sample; +// Creating a `LoggerProviderCollection` lets Serilog optionally write +// events through other dynamically-added MEL ILoggerProviders. +var providers = new LoggerProviderCollection(); -public class Program -{ - public static void Main(string[] args) - { - // 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(); +Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.Console() + .WriteTo.Providers(providers) + .CreateLogger(); - var services = new ServiceCollection(); +var services = new ServiceCollection(); - services.AddSingleton(providers); - services.AddSingleton(sc => - { - var providerCollection = sc.GetService(); - var factory = new SerilogLoggerFactory(null, true, providerCollection); +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); + foreach (var provider in sc.GetServices()) + factory.AddProvider(provider); - return factory; - }); + return factory; +}); - services.AddLogging(l => l.AddConsole()); +services.AddLogging(l => l.AddConsole()); - var serviceProvider = services.BuildServiceProvider(); - var logger = serviceProvider.GetRequiredService>(); +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); +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("Unexpected critical error starting application", ex); - 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("Unexpected error", ex); - logger.LogWarning("Unexpected warning", ex); - } +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 }); - } +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); +var endTime = DateTimeOffset.UtcNow; +logger.LogInformation(2, "Stopping at {StopTime}", endTime); - logger.LogInformation("Stopping"); +logger.LogInformation("Stopping"); - logger.LogInformation(Environment.NewLine); - 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); +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); - serviceProvider.Dispose(); - } -} +serviceProvider.Dispose(); diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index 44ba8e2..ef89f43 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -1,9 +1,10 @@  - net7 + net7.0 Sample Exe + enable @@ -16,4 +17,4 @@ - \ No newline at end of file + diff --git a/serilog-extensions-logging.sln.DotSettings b/serilog-extensions-logging.sln.DotSettings new file mode 100644 index 0000000..23a0f99 --- /dev/null +++ b/serilog-extensions-logging.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs index 294f201..8966ef7 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/CachingMessageTemplateParser.cs @@ -38,7 +38,7 @@ public MessageTemplate Parse(string messageTemplate) // ReSharper disable once InconsistentlySynchronizedField // ignored warning because this is by design - var result = (MessageTemplate)_templates[messageTemplate]; + var result = (MessageTemplate?)_templates[messageTemplate]; if (result != null) return result; diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/LevelConvert.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/LevelConvert.cs index c89c386..a77494e 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/LevelConvert.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/LevelConvert.cs @@ -27,9 +27,9 @@ public static class LevelConvert /// /// Convert to the equivalent Serilog . /// - /// A Microsoft.Extensions.Logging . + /// A Microsoft.Extensions.Logging . /// The Serilog equivalent of . - /// The value has no Serilog equivalent. It is mapped to + /// The value has no Serilog equivalent. It is mapped to /// as the closest approximation, but this has entirely /// different semantics. public static LogEventLevel ToSerilogLevel(LogLevel logLevel) @@ -46,7 +46,7 @@ public static LogEventLevel ToSerilogLevel(LogLevel logLevel) } /// - /// Convert to the equivalent Microsoft.Extensions.Logging . + /// Convert to the equivalent Microsoft.Extensions.Logging . /// /// A Serilog . /// The Microsoft.Extensions.Logging equivalent of . diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs index 930f457..20253f8 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogValues.cs @@ -17,14 +17,14 @@ namespace Serilog.Extensions.Logging; -readonly struct SerilogLogValues : IReadOnlyList> +readonly struct SerilogLogValues : IReadOnlyList> { // Note, this struct is only used in a very limited context internally, so we ignore // the possibility of fields being null via the default struct initialization. - private readonly MessageTemplate _messageTemplate; - private readonly IReadOnlyDictionary _properties; - private readonly KeyValuePair[] _values; + readonly MessageTemplate _messageTemplate; + readonly IReadOnlyDictionary _properties; + readonly KeyValuePair[] _values; public SerilogLogValues(MessageTemplate messageTemplate, IReadOnlyDictionary properties) { @@ -34,24 +34,24 @@ public SerilogLogValues(MessageTemplate messageTemplate, IReadOnlyDictionary interface expects indexed access - _values = new KeyValuePair[_properties.Count + 1]; + _values = new KeyValuePair[_properties.Count + 1]; var i = 0; foreach (var p in properties) { - _values[i] = new KeyValuePair(p.Key, (p.Value is ScalarValue sv) ? sv.Value : p.Value); + _values[i] = new KeyValuePair(p.Key, (p.Value is ScalarValue sv) ? sv.Value : p.Value); ++i; } - _values[i] = new KeyValuePair("{OriginalFormat}", _messageTemplate.Text); + _values[i] = new KeyValuePair("{OriginalFormat}", _messageTemplate.Text); } - public KeyValuePair this[int index] + public KeyValuePair this[int index] { get => _values[index]; } public int Count => _properties.Count + 1; - public IEnumerator> GetEnumerator() => ((IEnumerable>)_values).GetEnumerator(); + public IEnumerator> GetEnumerator() => ((IEnumerable>)_values).GetEnumerator(); public override string ToString() => _messageTemplate.Render(_properties); diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs index 1bf4bab..edcef30 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs @@ -41,10 +41,9 @@ public SerilogLogger( string? name = null) { _provider = provider ?? throw new ArgumentNullException(nameof(provider)); - _logger = logger!; // If a logger was passed, the provider has already added itself as an enricher - _logger ??= Serilog.Log.Logger.ForContext(new[] { provider }); + _logger = logger ?? Serilog.Log.Logger.ForContext(new[] { provider }); if (name != null) { @@ -57,12 +56,12 @@ public bool IsEnabled(LogLevel logLevel) return logLevel != LogLevel.None && _logger.IsEnabled(LevelConvert.ToSerilogLevel(logLevel)); } - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState : notnull { return _provider.BeginScope(state); } - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (logLevel == LogLevel.None) { @@ -81,7 +80,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } catch (Exception ex) { - SelfLog.WriteLine($"Failed to write event through {typeof(SerilogLogger).Name}: {ex}"); + SelfLog.WriteLine($"Failed to write event through {nameof(SerilogLogger)}: {ex}"); } // Do not swallow exceptions from here because Serilog takes care of them in case of WriteTo and throws them back to the caller in case of AuditTo. @@ -89,7 +88,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except _logger.Write(evt); } - LogEvent PrepareWrite(LogEventLevel level, EventId eventId, TState state, Exception exception, Func formatter) + LogEvent PrepareWrite(LogEventLevel level, EventId eventId, TState state, Exception? exception, Func formatter) { string? messageTemplate = null; @@ -139,7 +138,9 @@ LogEvent PrepareWrite(LogEventLevel level, EventId eventId, TState state propertyName = "State"; messageTemplate = "{State:l}"; } - else if (formatter != null) + // `formatter` was originally accepted as nullable, so despite the new annotation, this check should still + // be made. + else if (formatter != null!) { propertyName = "Message"; messageTemplate = "{Message:l}"; @@ -159,12 +160,12 @@ LogEvent PrepareWrite(LogEventLevel level, EventId eventId, TState state return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties); } - static object? AsLoggableValue(TState state, Func formatter) + static object? AsLoggableValue(TState state, Func? formatter) { - object? sobj = state; + object? stateObj = state; if (formatter != null) - sobj = formatter(state, null!); - return sobj; + stateObj = formatter(state, null); + return stateObj; } internal static LogEventProperty CreateEventIdProperty(EventId eventId) diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs index 2a46739..3fead38 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs @@ -66,7 +66,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) List? scopeItems = null; for (var scope = CurrentScope; scope != null; scope = scope.Parent) { - scope.EnrichAndCreateScopeItem(logEvent, propertyFactory, out LogEventPropertyValue? scopeItem); + scope.EnrichAndCreateScopeItem(logEvent, propertyFactory, out var scopeItem); if (scopeItem != null) { @@ -82,9 +82,9 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) } } - readonly AsyncLocal _value = new(); + readonly AsyncLocal _value = new(); - internal SerilogLoggerScope CurrentScope + internal SerilogLoggerScope? CurrentScope { get => _value.Value; set => _value.Value = value; diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs index f2d6c8a..9d13190 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs @@ -1,8 +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; -using System.Collections.Generic; using Serilog.Core; using Serilog.Events; @@ -29,7 +27,7 @@ public SerilogLoggerScope(SerilogLoggerProvider provider, object? state, IDispos _chainedDisposable = chainedDisposable; } - public SerilogLoggerScope Parent { get; } + public SerilogLoggerScope? Parent { get; } public void Dispose() { diff --git a/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs b/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs index 37885a5..50afb45 100644 --- a/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs +++ b/src/Serilog.Extensions.Logging/Properties/AssemblyInfo.cs @@ -1,7 +1,12 @@ -using System.Reflection; +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Threading; + +using System.Reflection; using System.Runtime.CompilerServices; -[assembly: AssemblyVersion("2.0.0.0")] +[assembly: AssemblyVersion("7.0.0.0")] [assembly: InternalsVisibleTo("Serilog.Extensions.Logging.Tests, PublicKey=" + "0024000004800000940000000602000000240000525341310004000001000100fb8d13fd344a1c" + diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 27d593b..f9da570 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -2,9 +2,10 @@ Low-level Serilog provider for Microsoft.Extensions.Logging - 3.1.1 + 7.0.0 Microsoft;Serilog Contributors - netstandard2.0 + + net462;netstandard2.0;netstandard2.1;net6.0;net7.0 true serilog;Microsoft.Extensions.Logging serilog-extension-nuget.png @@ -24,8 +25,9 @@ - - + + + 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 30050c0..24fdb78 100644 --- a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj +++ b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj @@ -1,7 +1,8 @@ - net7 + net7.0 + enable @@ -10,10 +11,7 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + 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 7650199..94c27dc 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -1,7 +1,8 @@ - net7;net472 + net7.0;net472 + enable @@ -10,10 +11,7 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/Serilog.Extensions.Logging.Tests/Support/DisposeTrackingLogger.cs b/test/Serilog.Extensions.Logging.Tests/Support/DisposeTrackingLogger.cs index 7c194d1..8effbfc 100644 --- a/test/Serilog.Extensions.Logging.Tests/Support/DisposeTrackingLogger.cs +++ b/test/Serilog.Extensions.Logging.Tests/Support/DisposeTrackingLogger.cs @@ -1,11 +1,12 @@ +using System.Diagnostics.CodeAnalysis; using Serilog.Core; using Serilog.Events; namespace Serilog.Extensions.Logging.Tests.Support; -public class DisposeTrackingLogger : ILogger, IDisposable +sealed class DisposeTrackingLogger : ILogger, IDisposable { - public bool IsDisposed { get; set; } + public bool IsDisposed { get; private set; } public ILogger ForContext(ILogEventEnricher enricher) { @@ -17,7 +18,7 @@ public ILogger ForContext(IEnumerable enrichers) return new LoggerConfiguration().CreateLogger(); } - public ILogger ForContext(string propertyName, object value, bool destructureObjects = false) + public ILogger ForContext(string propertyName, object? value, bool destructureObjects = false) { return new LoggerConfiguration().CreateLogger(); } @@ -53,29 +54,29 @@ public void Write(LogEventLevel level, string messageTemplate, T0 pr { } - public void Write(LogEventLevel level, string messageTemplate, params object[] propertyValues) + public void Write(LogEventLevel level, string messageTemplate, params object?[]? propertyValues) { } - public void Write(LogEventLevel level, Exception exception, string messageTemplate) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate) { } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T propertyValue) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T propertyValue) { } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, T0 propertyValue0, + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Write(LogEventLevel level, Exception exception, string messageTemplate, params object[] propertyValues) + public void Write(LogEventLevel level, Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -100,28 +101,28 @@ public void Verbose(string messageTemplate, T0 propertyValue0, T1 pr { } - public void Verbose(string messageTemplate, params object[] propertyValues) + public void Verbose(string messageTemplate, params object?[]? propertyValues) { } - public void Verbose(Exception exception, string messageTemplate) + public void Verbose(Exception? exception, string messageTemplate) { } - public void Verbose(Exception exception, string messageTemplate, T propertyValue) + public void Verbose(Exception? exception, string messageTemplate, T propertyValue) { } - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Verbose(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Verbose(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Verbose(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Verbose(Exception exception, string messageTemplate, params object[] propertyValues) + public void Verbose(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -141,28 +142,28 @@ public void Debug(string messageTemplate, T0 propertyValue0, T1 prop { } - public void Debug(string messageTemplate, params object[] propertyValues) + public void Debug(string messageTemplate, params object?[]? propertyValues) { } - public void Debug(Exception exception, string messageTemplate) + public void Debug(Exception? exception, string messageTemplate) { } - public void Debug(Exception exception, string messageTemplate, T propertyValue) + public void Debug(Exception? exception, string messageTemplate, T propertyValue) { } - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Debug(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Debug(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Debug(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Debug(Exception exception, string messageTemplate, params object[] propertyValues) + public void Debug(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -182,28 +183,28 @@ public void Information(string messageTemplate, T0 propertyValue0, T { } - public void Information(string messageTemplate, params object[] propertyValues) + public void Information(string messageTemplate, params object?[]? propertyValues) { } - public void Information(Exception exception, string messageTemplate) + public void Information(Exception? exception, string messageTemplate) { } - public void Information(Exception exception, string messageTemplate, T propertyValue) + public void Information(Exception? exception, string messageTemplate, T propertyValue) { } - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Information(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Information(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Information(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Information(Exception exception, string messageTemplate, params object[] propertyValues) + public void Information(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -223,28 +224,28 @@ public void Warning(string messageTemplate, T0 propertyValue0, T1 pr { } - public void Warning(string messageTemplate, params object[] propertyValues) + public void Warning(string messageTemplate, params object?[]? propertyValues) { } - public void Warning(Exception exception, string messageTemplate) + public void Warning(Exception? exception, string messageTemplate) { } - public void Warning(Exception exception, string messageTemplate, T propertyValue) + public void Warning(Exception? exception, string messageTemplate, T propertyValue) { } - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Warning(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Warning(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Warning(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Warning(Exception exception, string messageTemplate, params object[] propertyValues) + public void Warning(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -264,28 +265,28 @@ public void Error(string messageTemplate, T0 propertyValue0, T1 prop { } - public void Error(string messageTemplate, params object[] propertyValues) + public void Error(string messageTemplate, params object?[]? propertyValues) { } - public void Error(Exception exception, string messageTemplate) + public void Error(Exception? exception, string messageTemplate) { } - public void Error(Exception exception, string messageTemplate, T propertyValue) + public void Error(Exception? exception, string messageTemplate, T propertyValue) { } - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Error(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Error(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Error(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Error(Exception exception, string messageTemplate, params object[] propertyValues) + public void Error(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } @@ -305,40 +306,40 @@ public void Fatal(string messageTemplate, T0 propertyValue0, T1 prop { } - public void Fatal(string messageTemplate, params object[] propertyValues) + public void Fatal(string messageTemplate, params object?[]? propertyValues) { } - public void Fatal(Exception exception, string messageTemplate) + public void Fatal(Exception? exception, string messageTemplate) { } - public void Fatal(Exception exception, string messageTemplate, T propertyValue) + public void Fatal(Exception? exception, string messageTemplate, T propertyValue) { } - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) + public void Fatal(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1) { } - public void Fatal(Exception exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, + public void Fatal(Exception? exception, string messageTemplate, T0 propertyValue0, T1 propertyValue1, T2 propertyValue2) { } - public void Fatal(Exception exception, string messageTemplate, params object[] propertyValues) + public void Fatal(Exception? exception, string messageTemplate, params object?[]? propertyValues) { } - public bool BindMessageTemplate(string messageTemplate, object[] propertyValues, out MessageTemplate? parsedTemplate, - out IEnumerable? boundProperties) + public bool BindMessageTemplate(string messageTemplate, object?[]? propertyValues, [NotNullWhen(true)] out MessageTemplate? parsedTemplate, + [NotNullWhen(true)] out IEnumerable? boundProperties) { parsedTemplate = null; boundProperties = null; return false; } - public bool BindProperty(string propertyName, object value, bool destructureObjects, out LogEventProperty? property) + public bool BindProperty(string? propertyName, object? value, bool destructureObjects, [NotNullWhen(true)] out LogEventProperty? property) { property = null; return false; diff --git a/test/Serilog.Extensions.Logging.Tests/Support/ExtensionsProvider.cs b/test/Serilog.Extensions.Logging.Tests/Support/ExtensionsProvider.cs index 1fc2bb0..a27d0bd 100644 --- a/test/Serilog.Extensions.Logging.Tests/Support/ExtensionsProvider.cs +++ b/test/Serilog.Extensions.Logging.Tests/Support/ExtensionsProvider.cs @@ -5,14 +5,15 @@ namespace Serilog.Extensions.Logging.Tests.Support; -public class ExtensionsProvider : ILoggerProvider, Microsoft.Extensions.Logging.ILogger +sealed class ExtensionsProvider : ILoggerProvider, Microsoft.Extensions.Logging.ILogger { - private readonly LogLevel enabledLevel; - public List<(LogLevel logLevel, EventId eventId, object? state, Exception exception, string message)> Writes { get; set; } = new(); + readonly LogLevel _enabledLevel; + + public List<(LogLevel logLevel, EventId eventId, object? state, Exception? exception, string message)> Writes { get; } = new(); public ExtensionsProvider(LogLevel enabledLevel) { - this.enabledLevel = enabledLevel; + _enabledLevel = enabledLevel; } public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) @@ -20,17 +21,17 @@ public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName) return this; } - public IDisposable BeginScope(TState state) + public IDisposable BeginScope(TState state) where TState: notnull { return this; } public bool IsEnabled(LogLevel logLevel) { - return enabledLevel <= logLevel; + return _enabledLevel <= logLevel; } - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { Writes.Add((logLevel, eventId, state, exception, formatter(state, exception))); } From d525717f3d3ff36f3d8720c43930ea69a9185f95 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 14:59:01 +1000 Subject: [PATCH 2/6] Build script branch name change --- Build.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Build.ps1 b/Build.ps1 index 0687e20..a674b52 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -11,7 +11,7 @@ if(Test-Path .\artifacts) { $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 "master" -and $revision -ne "local"] +$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"] echo "build: Version suffix is $suffix" From 552e1428d008114defa92a0db65beb3ee941dffe Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 15:03:30 +1000 Subject: [PATCH 3/6] Notes on version matching --- .../Serilog.Extensions.Logging.csproj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index f9da570..af20b81 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -2,9 +2,11 @@ Low-level Serilog provider for Microsoft.Extensions.Logging + 7.0.0 Microsoft;Serilog Contributors - + net462;netstandard2.0;netstandard2.1;net6.0;net7.0 true serilog;Microsoft.Extensions.Logging @@ -26,6 +28,7 @@ + From 926d0f6ce2b609b2b528d38eeed75280d49e3450 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 15:06:40 +1000 Subject: [PATCH 4/6] Don't include untracked sources --- src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index af20b81..09c1cc0 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -18,7 +18,6 @@ git embedded true - true True README.md From 4bb0c388a49aeb0da847aa27c0d3966403d7aff5 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 15:19:14 +1000 Subject: [PATCH 5/6] Try a different approach to avoiding spurious NuGet pack issues --- .../Serilog.Extensions.Logging.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 09c1cc0..33d764b 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -18,8 +18,11 @@ git embedded true + true True README.md + + NU5118 From 5ba09d13e28b8aa16084e89aa82d7aace670bf51 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 3 May 2023 18:49:44 +1000 Subject: [PATCH 6/6] Quick README update to note the versioning policy --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 4c67766..bbed04e 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,10 @@ using (_logger.BeginScope(scopeProps) { // } ``` +### Versioning + +This package tracks the versioning and target framework support of its [_Microsoft.Extensions.Logging_](https://nuget.org/packages/Microsoft.Extensions.Logging) dependency. + ### Credits This package evolved from an earlier package _Microsoft.Framework.Logging.Serilog_ [provided by the ASP.NET team](https://github.com/aspnet/Logging/pull/182).