Skip to content

Commit c396419

Browse files
committed
Register diagnostic services; includes ILogger now in registered services since there's little reason not to provide it
1 parent ac4f4cd commit c396419

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ namespace Serilog.Extensions.Hosting
2222
/// </summary>
2323
public class DiagnosticContext : IDiagnosticContext
2424
{
25-
readonly ILogger _log;
25+
readonly ILogger _logger;
2626

2727
/// <summary>
2828
/// Construct a <see cref="DiagnosticContext"/>.
2929
/// </summary>
30-
/// <param name="log">A logger for binding properties in the context, or <c>null</c> to use <see cref="Log.Logger"/>.</param>
31-
public DiagnosticContext(ILogger log)
30+
/// <param name="logger">A logger for binding properties in the context, or <c>null</c> to use <see cref="Log.Logger"/>.</param>
31+
public DiagnosticContext(ILogger logger)
3232
{
33-
_log = log;
33+
_logger = logger;
3434
}
3535

3636
/// <summary>
@@ -50,7 +50,7 @@ public void Add(string propertyName, object value, bool destructureObjects = fal
5050

5151
var collector = AmbientDiagnosticContextCollector.Current;
5252
if (collector != null &&
53-
(_log ?? Log.Logger).BindProperty(propertyName, value, destructureObjects, out var property))
53+
(_logger ?? Log.Logger).BindProperty(propertyName, value, destructureObjects, out var property))
5454
{
5555
collector.Add(property);
5656
}

src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Extensions.Hosting;
1717
using Microsoft.Extensions.Logging;
1818
using Microsoft.Extensions.DependencyInjection;
19+
using Serilog.Extensions.Hosting;
1920
using Serilog.Extensions.Logging;
2021

2122
namespace Serilog
@@ -64,11 +65,7 @@ public static IHostBuilder UseSerilog(
6465
collection.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
6566
}
6667

67-
if (logger != null)
68-
{
69-
// This won't (and shouldn't) take ownership of the logger.
70-
collection.AddSingleton(logger);
71-
}
68+
ConfigureServices(collection, logger);
7269
});
7370

7471
return builder;
@@ -80,7 +77,7 @@ public static IHostBuilder UseSerilog(
8077
/// The logger will be shut down when application services are disposed.
8178
/// </remarks>
8279
/// <param name="builder">The host builder to configure.</param>
83-
/// <param name="configureLogger">The delegate for configuring the <see cref="LoggerConfiguration" /> that will be used to construct a <see cref="Logger" />.</param>
80+
/// <param name="configureLogger">The delegate for configuring the <see cref="LoggerConfiguration" /> that will be used to construct a <see cref="Microsoft.Extensions.Logging.Logger" />.</param>
8481
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Log.Logger"/>.</param>
8582
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
8683
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
@@ -109,9 +106,6 @@ public static IHostBuilder UseSerilog(
109106
configureLogger(context, loggerConfiguration);
110107
var logger = loggerConfiguration.CreateLogger();
111108

112-
// This won't (and shouldn't) take ownership of the logger.
113-
collection.AddSingleton(logger);
114-
115109
ILogger registeredLogger = null;
116110
if (preserveStaticLogger)
117111
{
@@ -136,8 +130,30 @@ public static IHostBuilder UseSerilog(
136130

137131
return factory;
138132
});
133+
134+
ConfigureServices(collection, logger);
139135
});
140136
return builder;
141137
}
138+
139+
static void ConfigureServices(IServiceCollection collection, ILogger logger)
140+
{
141+
if (collection == null) throw new ArgumentNullException(nameof(collection));
142+
143+
if (logger != null)
144+
{
145+
// This won't (and shouldn't) take ownership of the logger.
146+
collection.AddSingleton(logger);
147+
}
148+
149+
// Registered to provide two services...
150+
var diagnosticContext = new DiagnosticContext(logger);
151+
152+
// Consumed by e.g. middleware
153+
collection.AddSingleton(diagnosticContext);
154+
155+
// Consumed by user code
156+
collection.AddSingleton<IDiagnosticContext>(diagnosticContext);
157+
}
142158
}
143159
}

0 commit comments

Comments
 (0)