Skip to content

Commit 53999fd

Browse files
authored
Merge pull request #8 from nblumhardt/add-providers
Basic provider support
2 parents 99aa3c7 + 1f401ac commit 53999fd

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

samples/SimpleServiceSample/PrintTimeService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
66

7-
namespace SimpleWebSample
7+
namespace SimpleServiceSample
88
{
99
public class PrintTimeService : IHostedService, IDisposable
1010
{

samples/SimpleServiceSample/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.IO;
3-
using Microsoft.Extensions.Hosting;
43
using Microsoft.Extensions.Configuration;
5-
using Serilog;
64
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Serilog;
77

8-
namespace SimpleWebSample
8+
namespace SimpleServiceSample
99
{
1010
public class Program
1111
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Serilog.Extensions.Hosting/Hosting/SerilogLoggerFactory.cs renamed to src/Serilog.Extensions.Hosting/Extensions/Hosting/SerilogLoggerFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
1516
using Microsoft.Extensions.Logging;
1617
using Serilog.Debugging;
1718
using Serilog.Extensions.Logging;
1819

20+
// To line up with the convention used elsewhere in the *.Extensions libraries, this
21+
// should have been Serilog.Extensions.Hosting.
22+
// ReSharper disable once CheckNamespace
1923
namespace Serilog.Hosting
2024
{
2125
/// <summary>
2226
/// Implements <see cref="ILoggerFactory"/> so that we can inject Serilog Logger.
2327
/// </summary>
28+
[Obsolete("Replaced with Serilog.Extensions.Logging.SerilogLoggerFactory")]
2429
public class SerilogLoggerFactory : ILoggerFactory
2530
{
26-
private readonly SerilogLoggerProvider _provider;
31+
readonly SerilogLoggerProvider _provider;
2732

2833
/// <summary>
2934
/// Initializes a new instance of the <see cref="SerilogLoggerFactory"/> class.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Serilog support for .NET Core logging in hosted services</Description>
5-
<VersionPrefix>2.0.1</VersionPrefix>
5+
<VersionPrefix>3.0.0</VersionPrefix>
66
<Authors>Microsoft;Serilog Contributors</Authors>
77
<TargetFrameworks>netstandard2.0</TargetFrameworks>
88
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -23,8 +23,8 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Serilog" Version="2.5.0" />
27-
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.0" />
26+
<PackageReference Include="Serilog" Version="2.8.0" />
27+
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.0-*" />
2828
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.0" />
2929
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" />
3030
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.0" />

src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Serilog Contributors
1+
// Copyright 2019 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,8 +15,8 @@
1515
using System;
1616
using Microsoft.Extensions.Hosting;
1717
using Microsoft.Extensions.Logging;
18-
using Serilog.Hosting;
1918
using Microsoft.Extensions.DependencyInjection;
19+
using Serilog.Extensions.Logging;
2020

2121
namespace Serilog
2222
{
@@ -30,49 +30,103 @@ public static class SerilogHostBuilderExtensions
3030
/// </summary>
3131
/// <param name="builder">The host builder to configure.</param>
3232
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
33-
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
34-
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Log.CloseAndFlush()"/> method will be
33+
/// <param name="dispose">When true, dispose <paramref name="logger"/> when the framework disposes the provider. If the
34+
/// logger is not specified but <paramref name="dispose"/> is true, the <see cref="Log.CloseAndFlush()"/> method will be
3535
/// called on the static <see cref="Log"/> class instead.</param>
36-
/// <returns>The (generic) host builder.</returns>
37-
public static IHostBuilder UseSerilog(this IHostBuilder builder, Serilog.ILogger logger = null, bool dispose = false)
36+
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
37+
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="ILoggerProvider"/>s to receive events. By
38+
/// default, only Serilog sinks will receive events.</param>
39+
/// <returns>The host builder.</returns>
40+
public static IHostBuilder UseSerilog(
41+
this IHostBuilder builder,
42+
ILogger logger = null,
43+
bool dispose = false,
44+
LoggerProviderCollection providers = null)
3845
{
3946
if (builder == null) throw new ArgumentNullException(nameof(builder));
40-
builder.ConfigureServices((context, collection) =>
41-
collection.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose)));
47+
48+
builder.ConfigureServices((_, collection) =>
49+
{
50+
if (providers != null)
51+
{
52+
collection.AddSingleton<ILoggerFactory>(services =>
53+
{
54+
var factory = new SerilogLoggerFactory(logger, dispose, providers);
55+
56+
foreach (var provider in services.GetServices<ILoggerProvider>())
57+
factory.AddProvider(provider);
58+
59+
return factory;
60+
});
61+
}
62+
else
63+
{
64+
collection.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
65+
}
66+
});
67+
4268
return builder;
4369
}
4470

45-
/// <summary>
46-
/// Sets Serilog as the logging provider.
47-
/// </summary>
71+
/// <summary>Sets Serilog as the logging provider.</summary>
4872
/// <remarks>
4973
/// A <see cref="HostBuilderContext"/> is supplied so that configuration and hosting information can be used.
5074
/// The logger will be shut down when application services are disposed.
5175
/// </remarks>
5276
/// <param name="builder">The host builder to configure.</param>
5377
/// <param name="configureLogger">The delegate for configuring the <see cref="LoggerConfiguration" /> that will be used to construct a <see cref="Logger" />.</param>
5478
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of <see cref="Log.Logger"/>.</param>
55-
/// <returns>The (generic) host builder.</returns>
56-
public static IHostBuilder UseSerilog(this IHostBuilder builder, Action<HostBuilderContext, LoggerConfiguration> configureLogger, bool preserveStaticLogger = false)
79+
/// <param name="writeToProviders">By default, Serilog does not write events to <see cref="ILoggerProvider"/>s registered through
80+
/// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify
81+
/// <c>true</c> to write events to all providers.</param>
82+
/// <returns>The host builder.</returns>
83+
public static IHostBuilder UseSerilog(
84+
this IHostBuilder builder,
85+
Action<HostBuilderContext, LoggerConfiguration> configureLogger,
86+
bool preserveStaticLogger = false,
87+
bool writeToProviders = false)
5788
{
5889
if (builder == null) throw new ArgumentNullException(nameof(builder));
5990
if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger));
91+
6092
builder.ConfigureServices((context, collection) =>
6193
{
6294
var loggerConfiguration = new LoggerConfiguration();
95+
96+
LoggerProviderCollection loggerProviders = null;
97+
if (writeToProviders)
98+
{
99+
loggerProviders = new LoggerProviderCollection();
100+
loggerConfiguration.WriteTo.Providers(loggerProviders);
101+
}
102+
63103
configureLogger(context, loggerConfiguration);
64104
var logger = loggerConfiguration.CreateLogger();
105+
106+
ILogger registeredLogger = null;
65107
if (preserveStaticLogger)
66108
{
67-
collection.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, true));
109+
registeredLogger = logger;
68110
}
69-
else
111+
else
70112
{
71113
// Passing a `null` logger to `SerilogLoggerFactory` results in disposal via
72114
// `Log.CloseAndFlush()`, which additionally replaces the static logger with a no-op.
73115
Log.Logger = logger;
74-
collection.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(null, true));
75116
}
117+
118+
collection.AddSingleton<ILoggerFactory>(services =>
119+
{
120+
var factory = new SerilogLoggerFactory(registeredLogger, true, loggerProviders);
121+
122+
if (writeToProviders)
123+
{
124+
foreach (var provider in services.GetServices<ILoggerProvider>())
125+
factory.AddProvider(provider);
126+
}
127+
128+
return factory;
129+
});
76130
});
77131
return builder;
78132
}

0 commit comments

Comments
 (0)