Skip to content

Commit

Permalink
feat: add new host builder extensions and remove netstandard support
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 12, 2023
1 parent 74780bb commit 8bbd6d3
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 75 deletions.
5 changes: 2 additions & 3 deletions samples/SimpleServiceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public static int Main(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
.ConfigureLogging((context, logging) =>
logging.AddSerilog((conf, loggerConfiguration) => loggerConfiguration
.AddSerilog((conf, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(conf)
.Enrich.FromLogContext()
.WriteTo.Console()));
.WriteTo.Console());
}
}
7 changes: 3 additions & 4 deletions samples/WebApplicationSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Startup>(); });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Extensions for Serilog</Description>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
105 changes: 105 additions & 0 deletions src/Cnblogs.Serilog.Extensions/SerilogHostBuilderExtensions.cs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Serilog;
using Serilog.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using System;

namespace Microsoft.Extensions.Hosting;

/// <summary>
/// Extends <see cref="IHostBuilder"/> with Serilog configuration methods.
/// </summary>
public static class SerilogHostBuilderExtensions
{
/// <summary>
/// Sets Serilog as the logging provider.
/// </summary>
/// <param name="builder">The host builder to configure.</param>
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
/// called on the static <see cref="Log"/> class instead.</param>
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
/// default, only Serilog sinks will receive events.</param>
/// <returns>The host builder.</returns>
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;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The host builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The host builder.</returns>
public static IHostBuilder AddSerilog(
this IHostBuilder builder,
Action<IConfiguration, LoggerConfiguration> configureLogger,
bool preserveStaticLogger = false,
bool writeToProviders = false)
{
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
return builder;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The host builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The host builder.</returns>
public static IHostBuilder AddSerilog(
this IHostBuilder builder,
Action<LoggerConfiguration> configureLogger,
bool preserveStaticLogger = false,
bool writeToProviders = false)
{
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
return builder;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The host builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <remarks>If the static <see cref="Log.Logger"/> is a bootstrap logger (see
/// <c>LoggerConfigurationExtensions.CreateBootstrapLogger()</c>), and <paramref name="preserveStaticLogger"/> is
/// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being
/// replaced entirely or ignored.</remarks>
/// <returns>The host builder.</returns>
public static IHostBuilder AddSerilog(
this IHostBuilder builder,
Action<IConfiguration, IServiceProvider, LoggerConfiguration> configureLogger,
bool preserveStaticLogger = false,
bool writeToProviders = false)
{
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
return builder;
}
}
137 changes: 70 additions & 67 deletions src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,68 @@ public static ILoggingBuilder AddSerilog(
return builder;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Action<IConfiguration, LoggerConfiguration> 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);
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Action<LoggerConfiguration> 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);
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
Expand All @@ -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.</remarks>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilogFactory(
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Action<IServiceProvider, LoggerConfiguration> configureLogger,
Action<IConfiguration, IServiceProvider, LoggerConfiguration> configureLogger,
bool preserveStaticLogger = false,
bool writeToProviders = false)
{
Expand Down Expand Up @@ -115,7 +177,9 @@ public static ILoggingBuilder AddSerilogFactory(
if (loggerProviders != null)
cfg.WriteTo.Providers(loggerProviders);

configureLogger(sp, cfg);
var conf = sp.GetRequiredService<IConfiguration>();
configureLogger(conf, sp, cfg);

return cfg;
});

Expand All @@ -129,7 +193,9 @@ public static ILoggingBuilder AddSerilogFactory(
if (loggerProviders != null)
loggerConfiguration.WriteTo.Providers(loggerProviders);

configureLogger(sp, loggerConfiguration);
var conf = sp.GetRequiredService<IConfiguration>();
configureLogger(conf, sp, loggerConfiguration);

logger = loggerConfiguration.CreateLogger();
}

Expand Down Expand Up @@ -176,69 +242,6 @@ public static ILoggingBuilder AddSerilogFactory(
return builder;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Action<IConfiguration, LoggerConfiguration> 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<IConfiguration>();
configureLogger(configuration, loggerConfiguration);
},
preserveStaticLogger: preserveStaticLogger,
writeToProviders: writeToProviders);
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
/// The logger will be shut down when application services are disposed.
/// </remarks>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="configureLogger">The delegate for configuring the <see cref="Serilog.LoggerConfiguration" /> that will be used to construct a <see cref="Serilog.Core.Logger" />.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
/// <c>true</c> to write events to all providers.</param>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Action<LoggerConfiguration> 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));
Expand Down

0 comments on commit 8bbd6d3

Please sign in to comment.