Skip to content

Commit 1513f85

Browse files
authored
Merge pull request #9 from cnblogs/add-new-hostbuilder-extensions
feat: add new host builder extensions and remove netstandard support
2 parents 74780bb + 8bbd6d3 commit 1513f85

File tree

5 files changed

+181
-75
lines changed

5 files changed

+181
-75
lines changed

samples/SimpleServiceSample/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public static int Main(string[] args)
3636
public static IHostBuilder CreateHostBuilder(string[] args) =>
3737
Host.CreateDefaultBuilder(args)
3838
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
39-
.ConfigureLogging((context, logging) =>
40-
logging.AddSerilog((conf, loggerConfiguration) => loggerConfiguration
39+
.AddSerilog((conf, loggerConfiguration) => loggerConfiguration
4140
.ReadFrom.Configuration(conf)
4241
.Enrich.FromLogContext()
43-
.WriteTo.Console()));
42+
.WriteTo.Console());
4443
}
4544
}

samples/WebApplicationSample/Program.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public static int Main(string[] args)
4141

4242
public static IHostBuilder CreateHostBuilder(string[] args) =>
4343
Host.CreateDefaultBuilder(args)
44-
.ConfigureLogging((context, logging) =>
45-
logging.AddSerilogFactory((services, loggerConfig) => loggerConfig
44+
.AddSerilog((conf, services, loggerConfig) => loggerConfig
4645
.WriteTo.Console()
47-
.ReadFrom.Configuration(context.Configuration)
48-
.ReadFrom.Services(services)))
46+
.ReadFrom.Configuration(conf)
47+
.ReadFrom.Services(services))
4948
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
5049
}
5150
}

src/Cnblogs.Serilog.Extensions/Cnblogs.Serilog.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Description>Extensions for Serilog</Description>
55
<Authors>Serilog Contributors</Authors>
6-
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
6+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
77
<LangVersion>latest</LangVersion>
88
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Serilog;
2+
using Serilog.Extensions.Logging;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Configuration;
5+
using System;
6+
7+
namespace Microsoft.Extensions.Hosting;
8+
9+
/// <summary>
10+
/// Extends <see cref="IHostBuilder"/> with Serilog configuration methods.
11+
/// </summary>
12+
public static class SerilogHostBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Sets Serilog as the logging provider.
16+
/// </summary>
17+
/// <param name="builder">The host builder to configure.</param>
18+
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
19+
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
20+
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
21+
/// called on the static <see cref="Log"/> class instead.</param>
22+
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
23+
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
24+
/// default, only Serilog sinks will receive events.</param>
25+
/// <returns>The host builder.</returns>
26+
public static IHostBuilder AddSerilog(
27+
this IHostBuilder builder,
28+
Serilog.ILogger logger = null,
29+
bool dispose = false,
30+
LoggerProviderCollection providers = null)
31+
{
32+
builder.ConfigureLogging(logging => logging.AddSerilog(logger, dispose, providers));
33+
return builder;
34+
}
35+
36+
/// <summary>Sets Serilog as the logging provider.</summary>
37+
/// <remarks>
38+
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
39+
/// The logger will be shut down when application services are disposed.
40+
/// </remarks>
41+
/// <param name="builder">The host builder to configure.</param>
42+
/// <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>
43+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
44+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
45+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
46+
/// <c>true</c> to write events to all providers.</param>
47+
/// <returns>The host builder.</returns>
48+
public static IHostBuilder AddSerilog(
49+
this IHostBuilder builder,
50+
Action<IConfiguration, LoggerConfiguration> configureLogger,
51+
bool preserveStaticLogger = false,
52+
bool writeToProviders = false)
53+
{
54+
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
55+
return builder;
56+
}
57+
58+
/// <summary>Sets Serilog as the logging provider.</summary>
59+
/// <remarks>
60+
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
61+
/// The logger will be shut down when application services are disposed.
62+
/// </remarks>
63+
/// <param name="builder">The host builder to configure.</param>
64+
/// <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>
65+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
66+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
67+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
68+
/// <c>true</c> to write events to all providers.</param>
69+
/// <returns>The host builder.</returns>
70+
public static IHostBuilder AddSerilog(
71+
this IHostBuilder builder,
72+
Action<LoggerConfiguration> configureLogger,
73+
bool preserveStaticLogger = false,
74+
bool writeToProviders = false)
75+
{
76+
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
77+
return builder;
78+
}
79+
80+
/// <summary>Sets Serilog as the logging provider.</summary>
81+
/// <remarks>
82+
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
83+
/// The logger will be shut down when application services are disposed.
84+
/// </remarks>
85+
/// <param name="builder">The host builder to configure.</param>
86+
/// <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>
87+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
88+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
89+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
90+
/// <c>true</c> to write events to all providers.</param>
91+
/// <remarks>If the static <see cref="Log.Logger"/> is a bootstrap logger (see
92+
/// <c>LoggerConfigurationExtensions.CreateBootstrapLogger()</c>), and <paramref name="preserveStaticLogger"/> is
93+
/// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being
94+
/// replaced entirely or ignored.</remarks>
95+
/// <returns>The host builder.</returns>
96+
public static IHostBuilder AddSerilog(
97+
this IHostBuilder builder,
98+
Action<IConfiguration, IServiceProvider, LoggerConfiguration> configureLogger,
99+
bool preserveStaticLogger = false,
100+
bool writeToProviders = false)
101+
{
102+
builder.ConfigureLogging(logging => logging.AddSerilog(configureLogger, preserveStaticLogger, writeToProviders));
103+
return builder;
104+
}
105+
}

src/Cnblogs.Serilog.Extensions/SerilogLoggingBuilderExtensions.cs

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,68 @@ public static ILoggingBuilder AddSerilog(
6464
return builder;
6565
}
6666

67+
/// <summary>Sets Serilog as the logging provider.</summary>
68+
/// <remarks>
69+
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
70+
/// The logger will be shut down when application services are disposed.
71+
/// </remarks>
72+
/// <param name="builder">The logging builder to configure.</param>
73+
/// <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>
74+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
75+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
76+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
77+
/// <c>true</c> to write events to all providers.</param>
78+
/// <returns>The logging builder.</returns>
79+
public static ILoggingBuilder AddSerilog(
80+
this ILoggingBuilder builder,
81+
Action<IConfiguration, LoggerConfiguration> configureLogger,
82+
bool preserveStaticLogger = false,
83+
bool writeToProviders = false)
84+
{
85+
if (builder == null) throw new ArgumentNullException(nameof(builder));
86+
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
87+
88+
return AddSerilog(
89+
builder,
90+
(conf, sp, loggerConfiguration) =>
91+
{
92+
configureLogger(conf, loggerConfiguration);
93+
},
94+
preserveStaticLogger: preserveStaticLogger,
95+
writeToProviders: writeToProviders);
96+
}
97+
98+
/// <summary>Sets Serilog as the logging provider.</summary>
99+
/// <remarks>
100+
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
101+
/// The logger will be shut down when application services are disposed.
102+
/// </remarks>
103+
/// <param name="builder">The logging builder to configure.</param>
104+
/// <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>
105+
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
106+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
107+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
108+
/// <c>true</c> to write events to all providers.</param>
109+
/// <returns>The logging builder.</returns>
110+
public static ILoggingBuilder AddSerilog(
111+
this ILoggingBuilder builder,
112+
Action<LoggerConfiguration> configureLogger,
113+
bool preserveStaticLogger = false,
114+
bool writeToProviders = false)
115+
{
116+
if (builder == null) throw new ArgumentNullException(nameof(builder));
117+
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
118+
119+
return AddSerilog(
120+
builder,
121+
(sp, loggerConfiguration) =>
122+
{
123+
configureLogger(loggerConfiguration);
124+
},
125+
preserveStaticLogger: preserveStaticLogger,
126+
writeToProviders: writeToProviders);
127+
}
128+
67129
/// <summary>Sets Serilog as the logging provider.</summary>
68130
/// <remarks>
69131
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
@@ -80,9 +142,9 @@ public static ILoggingBuilder AddSerilog(
80142
/// not specified, the the bootstrap logger will be reconfigured through the supplied delegate, rather than being
81143
/// replaced entirely or ignored.</remarks>
82144
/// <returns>The logging builder.</returns>
83-
public static ILoggingBuilder AddSerilogFactory(
145+
public static ILoggingBuilder AddSerilog(
84146
this ILoggingBuilder builder,
85-
Action<IServiceProvider, LoggerConfiguration> configureLogger,
147+
Action<IConfiguration, IServiceProvider, LoggerConfiguration> configureLogger,
86148
bool preserveStaticLogger = false,
87149
bool writeToProviders = false)
88150
{
@@ -115,7 +177,9 @@ public static ILoggingBuilder AddSerilogFactory(
115177
if (loggerProviders != null)
116178
cfg.WriteTo.Providers(loggerProviders);
117179

118-
configureLogger(sp, cfg);
180+
var conf = sp.GetRequiredService<IConfiguration>();
181+
configureLogger(conf, sp, cfg);
182+
119183
return cfg;
120184
});
121185

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

132-
configureLogger(sp, loggerConfiguration);
196+
var conf = sp.GetRequiredService<IConfiguration>();
197+
configureLogger(conf, sp, loggerConfiguration);
198+
133199
logger = loggerConfiguration.CreateLogger();
134200
}
135201

@@ -176,69 +242,6 @@ public static ILoggingBuilder AddSerilogFactory(
176242
return builder;
177243
}
178244

179-
/// <summary>Sets Serilog as the logging provider.</summary>
180-
/// <remarks>
181-
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
182-
/// The logger will be shut down when application services are disposed.
183-
/// </remarks>
184-
/// <param name="builder">The logging builder to configure.</param>
185-
/// <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>
186-
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
187-
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
188-
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
189-
/// <c>true</c> to write events to all providers.</param>
190-
/// <returns>The logging builder.</returns>
191-
public static ILoggingBuilder AddSerilog(
192-
this ILoggingBuilder builder,
193-
Action<IConfiguration, LoggerConfiguration> configureLogger,
194-
bool preserveStaticLogger = false,
195-
bool writeToProviders = false)
196-
{
197-
if (builder == null) throw new ArgumentNullException(nameof(builder));
198-
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
199-
200-
return AddSerilogFactory(
201-
builder,
202-
(sp, loggerConfiguration) =>
203-
{
204-
var configuration = sp.GetRequiredService<IConfiguration>();
205-
configureLogger(configuration, loggerConfiguration);
206-
},
207-
preserveStaticLogger: preserveStaticLogger,
208-
writeToProviders: writeToProviders);
209-
}
210-
211-
/// <summary>Sets Serilog as the logging provider.</summary>
212-
/// <remarks>
213-
/// A <see cref="ILoggingBuilder"/> is supplied so that configuration and hosting information can be used.
214-
/// The logger will be shut down when application services are disposed.
215-
/// </remarks>
216-
/// <param name="builder">The logging builder to configure.</param>
217-
/// <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>
218-
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Serilog.Log.Logger"/>.</param>
219-
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
220-
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
221-
/// <c>true</c> to write events to all providers.</param>
222-
/// <returns>The logging builder.</returns>
223-
public static ILoggingBuilder AddSerilog(
224-
this ILoggingBuilder builder,
225-
Action<LoggerConfiguration> configureLogger,
226-
bool preserveStaticLogger = false,
227-
bool writeToProviders = false)
228-
{
229-
if (builder == null) throw new ArgumentNullException(nameof(builder));
230-
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
231-
232-
return AddSerilogFactory(
233-
builder,
234-
(sp, loggerConfiguration) =>
235-
{
236-
configureLogger(loggerConfiguration);
237-
},
238-
preserveStaticLogger: preserveStaticLogger,
239-
writeToProviders: writeToProviders);
240-
}
241-
242245
private static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger)
243246
{
244247
if (collection == null) throw new ArgumentNullException(nameof(collection));

0 commit comments

Comments
 (0)