diff --git a/samples/SimpleServiceSample/PrintTimeService.cs b/samples/SimpleServiceSample/PrintTimeService.cs index aa59fd0..513cace 100644 --- a/samples/SimpleServiceSample/PrintTimeService.cs +++ b/samples/SimpleServiceSample/PrintTimeService.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -namespace SimpleWebSample +namespace SimpleServiceSample { public class PrintTimeService : IHostedService, IDisposable { diff --git a/samples/SimpleServiceSample/Program.cs b/samples/SimpleServiceSample/Program.cs index b357b10..4ff6e7e 100644 --- a/samples/SimpleServiceSample/Program.cs +++ b/samples/SimpleServiceSample/Program.cs @@ -1,11 +1,11 @@ using System; using System.IO; -using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Configuration; -using Serilog; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Serilog; -namespace SimpleWebSample +namespace SimpleServiceSample { public class Program { diff --git a/serilog-extensions-hosting.sln.DotSettings b/serilog-extensions-hosting.sln.DotSettings new file mode 100644 index 0000000..1753e50 --- /dev/null +++ b/serilog-extensions-hosting.sln.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file diff --git a/src/Serilog.Extensions.Hosting/Hosting/SerilogLoggerFactory.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/SerilogLoggerFactory.cs similarity index 89% rename from src/Serilog.Extensions.Hosting/Hosting/SerilogLoggerFactory.cs rename to src/Serilog.Extensions.Hosting/Extensions/Hosting/SerilogLoggerFactory.cs index 1c91166..da5ddf7 100644 --- a/src/Serilog.Extensions.Hosting/Hosting/SerilogLoggerFactory.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/SerilogLoggerFactory.cs @@ -12,18 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using Microsoft.Extensions.Logging; using Serilog.Debugging; using Serilog.Extensions.Logging; +// To line up with the convention used elsewhere in the *.Extensions libraries, this +// should have been Serilog.Extensions.Hosting. +// ReSharper disable once CheckNamespace namespace Serilog.Hosting { /// /// Implements so that we can inject Serilog Logger. /// + [Obsolete("Replaced with Serilog.Extensions.Logging.SerilogLoggerFactory")] public class SerilogLoggerFactory : ILoggerFactory { - private readonly SerilogLoggerProvider _provider; + readonly SerilogLoggerProvider _provider; /// /// Initializes a new instance of the class. diff --git a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj index c343413..5a96a0a 100644 --- a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj +++ b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj @@ -2,7 +2,7 @@ Serilog support for .NET Core logging in hosted services - 2.0.1 + 3.0.0 Microsoft;Serilog Contributors netstandard2.0 true @@ -23,8 +23,8 @@ - - + + diff --git a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs index 804b8c3..23fecea 100644 --- a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs +++ b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs @@ -1,4 +1,4 @@ -// Copyright 2018 Serilog Contributors +// Copyright 2019 Serilog Contributors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,8 +15,8 @@ using System; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Serilog.Hosting; using Microsoft.Extensions.DependencyInjection; +using Serilog.Extensions.Logging; namespace Serilog { @@ -30,21 +30,45 @@ public static class SerilogHostBuilderExtensions /// /// The host builder to configure. /// The Serilog logger; if not supplied, the static will be used. - /// When true, dispose when the framework disposes the provider. If the - /// logger is not specified but is true, the method will be + /// When true, dispose when the framework disposes the provider. If the + /// logger is not specified but is true, the method will be /// called on the static class instead. - /// The (generic) host builder. - public static IHostBuilder UseSerilog(this IHostBuilder builder, Serilog.ILogger logger = null, bool dispose = false) + /// A registered in the Serilog pipeline using the + /// WriteTo.Providers() configuration method, enabling other s to receive events. By + /// default, only Serilog sinks will receive events. + /// The host builder. + public static IHostBuilder UseSerilog( + this IHostBuilder builder, + ILogger logger = null, + bool dispose = false, + LoggerProviderCollection providers = null) { if (builder == null) throw new ArgumentNullException(nameof(builder)); - builder.ConfigureServices((context, collection) => - collection.AddSingleton(services => new SerilogLoggerFactory(logger, dispose))); + + builder.ConfigureServices((_, collection) => + { + if (providers != null) + { + collection.AddSingleton(services => + { + var factory = new SerilogLoggerFactory(logger, dispose, providers); + + foreach (var provider in services.GetServices()) + factory.AddProvider(provider); + + return factory; + }); + } + else + { + collection.AddSingleton(services => new SerilogLoggerFactory(logger, dispose)); + } + }); + return builder; } - /// - /// Sets Serilog as the logging provider. - /// + /// Sets Serilog as the logging provider. /// /// A is supplied so that configuration and hosting information can be used. /// The logger will be shut down when application services are disposed. @@ -52,27 +76,57 @@ public static IHostBuilder UseSerilog(this IHostBuilder builder, Serilog.ILogger /// The host builder to configure. /// The delegate for configuring the that will be used to construct a . /// Indicates whether to preserve the value of . - /// The (generic) host builder. - public static IHostBuilder UseSerilog(this IHostBuilder builder, Action configureLogger, bool preserveStaticLogger = false) + /// By default, Serilog does not write events to s registered through + /// the Microsoft.Extensions.Logging API. Normally, equivalent Serilog sinks are used in place of providers. Specify + /// true to write events to all providers. + /// The host builder. + public static IHostBuilder UseSerilog( + this IHostBuilder builder, + Action configureLogger, + bool preserveStaticLogger = false, + bool writeToProviders = false) { if (builder == null) throw new ArgumentNullException(nameof(builder)); if (configureLogger == null) throw new ArgumentNullException(nameof(configureLogger)); + builder.ConfigureServices((context, collection) => { var loggerConfiguration = new LoggerConfiguration(); + + LoggerProviderCollection loggerProviders = null; + if (writeToProviders) + { + loggerProviders = new LoggerProviderCollection(); + loggerConfiguration.WriteTo.Providers(loggerProviders); + } + configureLogger(context, loggerConfiguration); var logger = loggerConfiguration.CreateLogger(); + + ILogger registeredLogger = null; if (preserveStaticLogger) { - collection.AddSingleton(services => new SerilogLoggerFactory(logger, true)); + registeredLogger = logger; } - else + else { // Passing a `null` logger to `SerilogLoggerFactory` results in disposal via // `Log.CloseAndFlush()`, which additionally replaces the static logger with a no-op. Log.Logger = logger; - collection.AddSingleton(services => new SerilogLoggerFactory(null, true)); } + + collection.AddSingleton(services => + { + var factory = new SerilogLoggerFactory(registeredLogger, true, loggerProviders); + + if (writeToProviders) + { + foreach (var provider in services.GetServices()) + factory.AddProvider(provider); + } + + return factory; + }); }); return builder; }