From 8bbd6d378e8af8df438af84e5ab969162872cde2 Mon Sep 17 00:00:00 2001 From: dudu Date: Sun, 12 Feb 2023 18:46:52 +0800 Subject: [PATCH] feat: add new host builder extensions and remove netstandard support --- samples/SimpleServiceSample/Program.cs | 5 +- samples/WebApplicationSample/Program.cs | 7 +- .../Cnblogs.Serilog.Extensions.csproj | 2 +- .../SerilogHostBuilderExtensions.cs.cs | 105 ++++++++++++++ .../SerilogLoggingBuilderExtensions.cs | 137 +++++++++--------- 5 files changed, 181 insertions(+), 75 deletions(-) create mode 100644 src/Cnblogs.Serilog.Extensions/SerilogHostBuilderExtensions.cs.cs diff --git a/samples/SimpleServiceSample/Program.cs b/samples/SimpleServiceSample/Program.cs index 8cbf6bd..1ced70f 100644 --- a/samples/SimpleServiceSample/Program.cs +++ b/samples/SimpleServiceSample/Program.cs @@ -36,10 +36,9 @@ public static int Main(string[] args) public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices(services => services.AddHostedService()) - .ConfigureLogging((context, logging) => - logging.AddSerilog((conf, loggerConfiguration) => loggerConfiguration + .AddSerilog((conf, loggerConfiguration) => loggerConfiguration .ReadFrom.Configuration(conf) .Enrich.FromLogContext() - .WriteTo.Console())); + .WriteTo.Console()); } } \ No newline at end of file diff --git a/samples/WebApplicationSample/Program.cs b/samples/WebApplicationSample/Program.cs index b95866d..8cc989a 100644 --- a/samples/WebApplicationSample/Program.cs +++ b/samples/WebApplicationSample/Program.cs @@ -41,11 +41,10 @@ public static int Main(string[] args) public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) - .ConfigureLogging((context, logging) => - logging.AddSerilogFactory((services, loggerConfig) => loggerConfig + .AddSerilog((conf, services, loggerConfig) => loggerConfig .WriteTo.Console() - .ReadFrom.Configuration(context.Configuration) - .ReadFrom.Services(services))) + .ReadFrom.Configuration(conf) + .ReadFrom.Services(services)) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } } \ No newline at end of file diff --git a/src/Cnblogs.Serilog.Extensions/Cnblogs.Serilog.Extensions.csproj b/src/Cnblogs.Serilog.Extensions/Cnblogs.Serilog.Extensions.csproj index cbe6abd..dd0582d 100644 --- a/src/Cnblogs.Serilog.Extensions/Cnblogs.Serilog.Extensions.csproj +++ b/src/Cnblogs.Serilog.Extensions/Cnblogs.Serilog.Extensions.csproj @@ -3,7 +3,7 @@ Extensions for Serilog Serilog Contributors - netstandard2.0;netstandard2.1;net6.0;net7.0 + net6.0;net7.0 latest true true diff --git a/src/Cnblogs.Serilog.Extensions/SerilogHostBuilderExtensions.cs.cs b/src/Cnblogs.Serilog.Extensions/SerilogHostBuilderExtensions.cs.cs new file mode 100644 index 0000000..72b2ca7 --- /dev/null +++ b/src/Cnblogs.Serilog.Extensions/SerilogHostBuilderExtensions.cs.cs @@ -0,0 +1,105 @@ +using Serilog; +using Serilog.Extensions.Logging; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Configuration; +using System; + +namespace Microsoft.Extensions.Hosting; + +/// +/// Extends with Serilog configuration methods. +/// +public static class SerilogHostBuilderExtensions +{ + /// + /// Sets Serilog as the logging provider. + /// + /// The host 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 IHostBuilder AddSerilog( + this IHostBuilder builder, + Serilog.ILogger logger = null, + bool dispose = false, + LoggerProviderCollection providers = null) + { + builder.ConfigureLogging(logging => logging.AddSerilog(logger, dispose, providers)); + return builder; + } + + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The host builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// By default, Serilog does not write events to s 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 host builder. + public static IHostBuilder AddSerilog( + this IHostBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) + { + builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders)); + return builder; + } + + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The host builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// By default, Serilog does not write events to s 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 host builder. + public static IHostBuilder AddSerilog( + this IHostBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) + { + builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders)); + return builder; + } + + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The host builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// By default, Serilog does not write events to s 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. + /// If the static is a bootstrap logger (see + /// LoggerConfigurationExtensions.CreateBootstrapLogger()), and is + /// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being + /// replaced entirely or ignored. + /// The host builder. + public static IHostBuilder AddSerilog( + this IHostBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) + { + builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders)); + return builder; + } +} \ No newline at end of file diff --git a/src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs b/src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs index aa171a6..72772cb 100644 --- a/src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs +++ b/src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs @@ -64,6 +64,68 @@ public static ILoggingBuilder AddSerilog( return builder; } + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The logging builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// By default, Serilog does not write events to s 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 AddSerilog( + this ILoggingBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); + + return AddSerilog( + builder, + (conf, sp, loggerConfiguration) => + { + configureLogger(conf, loggerConfiguration); + }, + preserveStaticLogger: preserveStaticLogger, + writeToProviders: writeToProviders); + } + + /// Sets Serilog as the logging provider. + /// + /// A is supplied so that configuration and hosting information can be used. + /// The logger will be shut down when application services are disposed. + /// + /// The logging builder to configure. + /// The delegate for configuring the that will be used to construct a . + /// Indicates whether to preserve the value of . + /// By default, Serilog does not write events to s 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 AddSerilog( + this ILoggingBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) + { + if (builder == null) throw new ArgumentNullException(nameof(builder)); + if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); + + return AddSerilog( + builder, + (sp, loggerConfiguration) => + { + configureLogger(loggerConfiguration); + }, + preserveStaticLogger: preserveStaticLogger, + writeToProviders: writeToProviders); + } + /// Sets Serilog as the logging provider. /// /// A is supplied so that configuration and hosting information can be used. @@ -80,9 +142,9 @@ public static ILoggingBuilder AddSerilog( /// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being /// replaced entirely or ignored. /// The logging builder. - public static ILoggingBuilder AddSerilogFactory( + public static ILoggingBuilder AddSerilog( this ILoggingBuilder builder, - Action configureLogger, + Action configureLogger, bool preserveStaticLogger = false, bool writeToProviders = false) { @@ -115,7 +177,9 @@ public static ILoggingBuilder AddSerilogFactory( if (loggerProviders != null) cfg.WriteTo.Providers(loggerProviders); - configureLogger(sp, cfg); + var conf = sp.GetRequiredService(); + configureLogger(conf, sp, cfg); + return cfg; }); @@ -129,7 +193,9 @@ public static ILoggingBuilder AddSerilogFactory( if (loggerProviders != null) loggerConfiguration.WriteTo.Providers(loggerProviders); - configureLogger(sp, loggerConfiguration); + var conf = sp.GetRequiredService(); + configureLogger(conf, sp, loggerConfiguration); + logger = loggerConfiguration.CreateLogger(); } @@ -176,69 +242,6 @@ public static ILoggingBuilder AddSerilogFactory( return builder; } - /// Sets Serilog as the logging provider. - /// - /// A is supplied so that configuration and hosting information can be used. - /// The logger will be shut down when application services are disposed. - /// - /// The logging builder to configure. - /// The delegate for configuring the that will be used to construct a . - /// Indicates whether to preserve the value of . - /// By default, Serilog does not write events to s 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 AddSerilog( - this ILoggingBuilder builder, - Action configureLogger, - bool preserveStaticLogger = false, - bool writeToProviders = false) - { - if (builder == null) throw new ArgumentNullException(nameof(builder)); - if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); - - return AddSerilogFactory( - builder, - (sp, loggerConfiguration) => - { - var configuration = sp.GetRequiredService(); - configureLogger(configuration, loggerConfiguration); - }, - preserveStaticLogger: preserveStaticLogger, - writeToProviders: writeToProviders); - } - - /// Sets Serilog as the logging provider. - /// - /// A is supplied so that configuration and hosting information can be used. - /// The logger will be shut down when application services are disposed. - /// - /// The logging builder to configure. - /// The delegate for configuring the that will be used to construct a . - /// Indicates whether to preserve the value of . - /// By default, Serilog does not write events to s 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 AddSerilog( - this ILoggingBuilder builder, - Action configureLogger, - bool preserveStaticLogger = false, - bool writeToProviders = false) - { - if (builder == null) throw new ArgumentNullException(nameof(builder)); - if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); - - return AddSerilogFactory( - builder, - (sp, loggerConfiguration) => - { - configureLogger(loggerConfiguration); - }, - preserveStaticLogger: preserveStaticLogger, - writeToProviders: writeToProviders); - } - private static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger) { if (collection == null) throw new ArgumentNullException(nameof(collection));