Skip to content

Commit 62bb7db

Browse files
committed
refactor: move AddSerilog to separate class
1 parent 874078a commit 62bb7db

File tree

3 files changed

+69
-56
lines changed

3 files changed

+69
-56
lines changed

src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Serilog support for .NET Core logging in hosted services</Description>
55
<VersionPrefix>5.0.1</VersionPrefix>
66
<Authors>Microsoft;Serilog Contributors</Authors>
77
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
8-
<LangVersion>8</LangVersion>
8+
<LangVersion>latest</LangVersion>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Serilog.Extensions.Hosting</AssemblyName>

src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static class SerilogHostBuilderExtensions
3232
// root logger, registering it as a singleton may lead to disposal along with the container by MEDI. This isn't
3333
// always desirable, i.e. we may be handed a logger and `dispose: false`, so wrapping it keeps us in control
3434
// of when the logger is disposed.
35-
private class RegisteredLogger
35+
internal class RegisteredLogger
3636
{
3737
public RegisteredLogger(ILogger logger)
3838
{
@@ -64,58 +64,6 @@ public static IHostBuilder UseSerilog(
6464
services.AddLogging(logging => logging.AddSerilog(logger, dispose, providers)));
6565
}
6666

67-
/// <summary>
68-
/// Sets Serilog as the logging provider.
69-
/// </summary>
70-
/// <param name="builder">The logging builder to configure.</param>
71-
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
72-
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
73-
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
74-
/// called on the static <see cref="Serilog.Log"/> class instead.</param>
75-
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
76-
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
77-
/// default, only Serilog sinks will receive events.</param>
78-
/// <returns>The host builder.</returns>
79-
public static ILoggingBuilder AddSerilog(
80-
this ILoggingBuilder builder,
81-
ILogger logger = null,
82-
bool dispose = false,
83-
LoggerProviderCollection providers = null)
84-
{
85-
if (builder == null) throw new ArgumentNullException(nameof(builder));
86-
87-
var services = builder.Services;
88-
if (providers != null)
89-
{
90-
services.AddSingleton<ILoggerFactory>(services =>
91-
{
92-
var factory = new SerilogLoggerFactory(logger, dispose, providers);
93-
94-
foreach (var provider in services.GetServices<ILoggerProvider>())
95-
factory.AddProvider(provider);
96-
97-
return factory;
98-
});
99-
}
100-
else
101-
{
102-
services.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
103-
}
104-
105-
if (logger != null)
106-
{
107-
// This won't (and shouldn't) take ownership of the logger.
108-
services.AddSingleton(logger);
109-
110-
// Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext.
111-
services.AddSingleton(new RegisteredLogger(logger));
112-
}
113-
bool useRegisteredLogger = logger != null;
114-
ConfigureDiagnosticContext(services, useRegisteredLogger);
115-
116-
return builder;
117-
}
118-
11967
/// <summary>Sets Serilog as the logging provider.</summary>
12068
/// <remarks>
12169
/// A <see cref="HostBuilderContext"/> is supplied so that configuration and hosting information can be used.
@@ -258,7 +206,7 @@ public static IHostBuilder UseSerilog(
258206
return builder;
259207
}
260208

261-
private static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger)
209+
internal static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger)
262210
{
263211
if (collection == null) throw new ArgumentNullException(nameof(collection));
264212

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Serilog.Extensions.Logging;
3+
using System;
4+
using static Serilog.SerilogHostBuilderExtensions;
5+
6+
namespace Microsoft.Extensions.Logging
7+
{
8+
/// <summary>
9+
/// Extends <see cref="ILoggingBuilder"/> with Serilog configuration methods.
10+
/// </summary>
11+
public static class SerilogLoggingBuilderExtensions
12+
{
13+
/// <summary>
14+
/// Sets Serilog as the logging provider.
15+
/// </summary>
16+
/// <param name="builder">The logging builder to configure.</param>
17+
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
18+
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
19+
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
20+
/// called on the static <see cref="Serilog.Log"/> class instead.</param>
21+
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
22+
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
23+
/// default, only Serilog sinks will receive events.</param>
24+
/// <returns>The host builder.</returns>
25+
public static ILoggingBuilder AddSerilog(
26+
this ILoggingBuilder builder,
27+
Serilog.ILogger logger = null,
28+
bool dispose = false,
29+
LoggerProviderCollection providers = null)
30+
{
31+
if (builder == null) throw new ArgumentNullException(nameof(builder));
32+
33+
var services = builder.Services;
34+
if (providers != null)
35+
{
36+
services.AddSingleton<ILoggerFactory>(services =>
37+
{
38+
var factory = new SerilogLoggerFactory(logger, dispose, providers);
39+
40+
foreach (var provider in services.GetServices<ILoggerProvider>())
41+
factory.AddProvider(provider);
42+
43+
return factory;
44+
});
45+
}
46+
else
47+
{
48+
services.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
49+
}
50+
51+
if (logger != null)
52+
{
53+
// This won't (and shouldn't) take ownership of the logger.
54+
services.AddSingleton(logger);
55+
56+
// Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext.
57+
services.AddSingleton(new RegisteredLogger(logger));
58+
}
59+
bool useRegisteredLogger = logger != null;
60+
ConfigureDiagnosticContext(services, useRegisteredLogger);
61+
62+
return builder;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)