From 2201fc0d80462f5c628b240e274217cba8a5b6f5 Mon Sep 17 00:00:00 2001 From: Jon Sagara Date: Fri, 1 Sep 2023 08:19:18 -0700 Subject: [PATCH] Added a Serilog extension to work with the new HostApplicationBuilder pattern. --- Directory.Build.props | 2 +- .../Sagara.Core.Logging.Serilog.csproj | 2 + .../SerilogExtensions.cs | 42 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/Sagara.Core.Logging.Serilog/SerilogExtensions.cs diff --git a/Directory.Build.props b/Directory.Build.props index 84988fa..b18fdb8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ - 1.0.0-alpha4 + 1.0.0-alpha5 1.0.0 1.0.0 Jon Sagara diff --git a/src/Sagara.Core.Logging.Serilog/Sagara.Core.Logging.Serilog.csproj b/src/Sagara.Core.Logging.Serilog/Sagara.Core.Logging.Serilog.csproj index c7df9ca..7c0b792 100644 --- a/src/Sagara.Core.Logging.Serilog/Sagara.Core.Logging.Serilog.csproj +++ b/src/Sagara.Core.Logging.Serilog/Sagara.Core.Logging.Serilog.csproj @@ -25,7 +25,9 @@ + + diff --git a/src/Sagara.Core.Logging.Serilog/SerilogExtensions.cs b/src/Sagara.Core.Logging.Serilog/SerilogExtensions.cs new file mode 100644 index 0000000..8b408c5 --- /dev/null +++ b/src/Sagara.Core.Logging.Serilog/SerilogExtensions.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; + +namespace Sagara.Core.Logging.Serilog; + +public static class SerilogExtensions +{ + /// + /// Sets Serilog as the logging provider. + /// + /// + /// This is (hopefully) a temporary replacement for Serilog.Extensions.Hosting.UseSerilog. It's necessary + /// to support working with the new Host.CreateApplicationBuilder() pattern being rolled out in .NET 7, which + /// does not currently support the IHostBuilder interface. + /// See: https://github.com/dotnet/runtime/discussions/81090#discussioncomment-4784551 + /// + /// The logging builder to configure. + /// The IConfiguration from which Serilog will attempt to read its configuration. + /// The delegate for configuring the Serilog.LoggerConfiguration that will be used + /// to construct a Serilog.Core.Logger. + /// Indicates whether to preserve the value of Serilog.Log.Logger. + /// By default, Serilog does not write events to Microsoft.Extensions.Logging.ILoggerProviders + /// registered through the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. + /// Specify true to write events to all providers. + /// The logging builder. + public static ILoggingBuilder UseSerilog(this ILoggingBuilder builder, IConfiguration config, Action configureLogger, bool preserveStaticLogger = false, bool writeToProviders = false) + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configureLogger); + + IConfiguration config2 = config; + + builder.Services.AddSerilog( + (serviceProvider, loggerConfiguration) => configureLogger(config2, serviceProvider, loggerConfiguration), + preserveStaticLogger: preserveStaticLogger, + writeToProviders: writeToProviders + ); + + return builder; + } +}