From 62bb7db9211784321f9c1c848ab99cba0588e2e6 Mon Sep 17 00:00:00 2001 From: dudu Date: Sun, 12 Feb 2023 08:04:14 +0800 Subject: [PATCH] refactor: move AddSerilog to separate class --- .../Serilog.Extensions.Hosting.csproj | 4 +- .../SerilogHostBuilderExtensions.cs | 56 +--------------- .../SerilogLoggingBuilderExtensions.cs | 65 +++++++++++++++++++ 3 files changed, 69 insertions(+), 56 deletions(-) create mode 100644 src/Serilog.Extensions.Hosting/SerilogLoggingBuilderExtensions.cs diff --git a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj index f8ef4a3..f95bf72 100644 --- a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj +++ b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj @@ -1,11 +1,11 @@ - + Serilog support for .NET Core logging in hosted services 5.0.1 Microsoft;Serilog Contributors netstandard2.0;netstandard2.1;net6.0;net7.0 - 8 + latest true true Serilog.Extensions.Hosting diff --git a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs index c9d8c97..2a66cdd 100644 --- a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs +++ b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs @@ -32,7 +32,7 @@ public static class SerilogHostBuilderExtensions // root logger, registering it as a singleton may lead to disposal along with the container by MEDI. This isn't // always desirable, i.e. we may be handed a logger and `dispose: false`, so wrapping it keeps us in control // of when the logger is disposed. - private class RegisteredLogger + internal class RegisteredLogger { public RegisteredLogger(ILogger logger) { @@ -64,58 +64,6 @@ public static IHostBuilder UseSerilog( services.AddLogging(logging => logging.AddSerilog(logger, dispose, providers))); } - /// - /// Sets Serilog as the logging provider. - /// - /// The logging builder to configure. - /// The Serilog logger; if not supplied, the static will be used. - /// When true, dispose when the framework disposes the provider. If the - /// logger is not specified but is true, the method will be - /// called on the static class instead. - /// A registered in the Serilog pipeline using the - /// WriteTo.Providers() configuration method, enabling other s to receive events. By - /// default, only Serilog sinks will receive events. - /// The host builder. - public static ILoggingBuilder AddSerilog( - this ILoggingBuilder builder, - ILogger logger = null, - bool dispose = false, - LoggerProviderCollection providers = null) - { - if (builder == null) throw new ArgumentNullException(nameof(builder)); - - var services = builder.Services; - if (providers != null) - { - services.AddSingleton(services => - { - var factory = new SerilogLoggerFactory(logger, dispose, providers); - - foreach (var provider in services.GetServices()) - factory.AddProvider(provider); - - return factory; - }); - } - else - { - services.AddSingleton(services => new SerilogLoggerFactory(logger, dispose)); - } - - if (logger != null) - { - // This won't (and shouldn't) take ownership of the logger. - services.AddSingleton(logger); - - // Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext. - services.AddSingleton(new RegisteredLogger(logger)); - } - bool useRegisteredLogger = logger != null; - ConfigureDiagnosticContext(services, useRegisteredLogger); - - return builder; - } - /// Sets Serilog as the logging provider. /// /// A is supplied so that configuration and hosting information can be used. @@ -258,7 +206,7 @@ public static IHostBuilder UseSerilog( return builder; } - private static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger) + internal static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger) { if (collection == null) throw new ArgumentNullException(nameof(collection)); diff --git a/src/Serilog.Extensions.Hosting/SerilogLoggingBuilderExtensions.cs b/src/Serilog.Extensions.Hosting/SerilogLoggingBuilderExtensions.cs new file mode 100644 index 0000000..cf8bf04 --- /dev/null +++ b/src/Serilog.Extensions.Hosting/SerilogLoggingBuilderExtensions.cs @@ -0,0 +1,65 @@ +using Microsoft.Extensions.DependencyInjection; +using Serilog.Extensions.Logging; +using System; +using static Serilog.SerilogHostBuilderExtensions; + +namespace Microsoft.Extensions.Logging +{ + /// + /// Extends with Serilog configuration methods. + /// + public static class SerilogLoggingBuilderExtensions + { + /// + /// Sets Serilog as the logging provider. + /// + /// The logging builder to configure. + /// The Serilog logger; if not supplied, the static will be used. + /// When true, dispose when the framework disposes the provider. If the + /// logger is not specified but is true, the method will be + /// called on the static class instead. + /// A registered in the Serilog pipeline using the + /// WriteTo.Providers() configuration method, enabling other s to receive events. By + /// default, only Serilog sinks will receive events. + /// The host builder. + public static ILoggingBuilder AddSerilog( + this ILoggingBuilder builder, + Serilog.ILogger logger = null, + bool dispose = false, + LoggerProviderCollection providers = null) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + + var services = builder.Services; + if (providers != null) + { + services.AddSingleton(services => + { + var factory = new SerilogLoggerFactory(logger, dispose, providers); + + foreach (var provider in services.GetServices()) + factory.AddProvider(provider); + + return factory; + }); + } + else + { + services.AddSingleton(services => new SerilogLoggerFactory(logger, dispose)); + } + + if (logger != null) + { + // This won't (and shouldn't) take ownership of the logger. + services.AddSingleton(logger); + + // Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext. + services.AddSingleton(new RegisteredLogger(logger)); + } + bool useRegisteredLogger = logger != null; + ConfigureDiagnosticContext(services, useRegisteredLogger); + + return builder; + } + } +} \ No newline at end of file