Skip to content

Commit

Permalink
refactor: move AddSerilog to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 12, 2023
1 parent 874078a commit 62bb7db
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Serilog support for .NET Core logging in hosted services</Description>
<VersionPrefix>5.0.1</VersionPrefix>
<Authors>Microsoft;Serilog Contributors</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<LangVersion>8</LangVersion>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Serilog.Extensions.Hosting</AssemblyName>
Expand Down
56 changes: 2 additions & 54 deletions src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class SerilogHostBuilderExtensions
// root logger, registering it as a singleton may lead to disposal along with the container by MEDI. This isn't
// always desirable, i.e. we may be handed a logger and `dispose: false`, so wrapping it keeps us in control
// of when the logger is disposed.
private class RegisteredLogger
internal class RegisteredLogger
{
public RegisteredLogger(ILogger logger)
{
Expand Down Expand Up @@ -64,58 +64,6 @@ public static IHostBuilder UseSerilog(
services.AddLogging(logging => logging.AddSerilog(logger, dispose, providers)));
}

/// <summary>
/// Sets Serilog as the logging provider.
/// </summary>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
/// called on the static <see cref="Serilog.Log"/> class instead.</param>
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
/// default, only Serilog sinks will receive events.</param>
/// <returns>The host builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
ILogger logger = null,
bool dispose = false,
LoggerProviderCollection providers = null)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));

var services = builder.Services;
if (providers != null)
{
services.AddSingleton<ILoggerFactory>(services =>
{
var factory = new SerilogLoggerFactory(logger, dispose, providers);

foreach (var provider in services.GetServices<ILoggerProvider>())
factory.AddProvider(provider);

return factory;
});
}
else
{
services.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
}

if (logger != null)
{
// This won't (and shouldn't) take ownership of the logger.
services.AddSingleton(logger);

// Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext.
services.AddSingleton(new RegisteredLogger(logger));
}
bool useRegisteredLogger = logger != null;
ConfigureDiagnosticContext(services, useRegisteredLogger);

return builder;
}

/// <summary>Sets Serilog as the logging provider.</summary>
/// <remarks>
/// A <see cref="HostBuilderContext"/> is supplied so that configuration and hosting information can be used.
Expand Down Expand Up @@ -258,7 +206,7 @@ public static IHostBuilder UseSerilog(
return builder;
}

private static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger)
internal static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger)
{
if (collection == null) throw new ArgumentNullException(nameof(collection));

Expand Down
65 changes: 65 additions & 0 deletions src/Serilog.Extensions.Hosting/SerilogLoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.Extensions.DependencyInjection;
using Serilog.Extensions.Logging;
using System;
using static Serilog.SerilogHostBuilderExtensions;

namespace Microsoft.Extensions.Logging
{
/// <summary>
/// Extends <see cref="ILoggingBuilder"/> with Serilog configuration methods.
/// </summary>
public static class SerilogLoggingBuilderExtensions
{
/// <summary>
/// Sets Serilog as the logging provider.
/// </summary>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="logger">The Serilog logger; if not supplied, the static <see cref="Serilog.Log"/> will be used.</param>
/// <param name="dispose">When <c>true</c>, dispose <paramref name="logger"/> when the framework disposes the provider. If the
/// logger is not specified but <paramref name="dispose"/> is <c>true</c>, the <see cref="Serilog.Log.CloseAndFlush()"/> method will be
/// called on the static <see cref="Serilog.Log"/> class instead.</param>
/// <param name="providers">A <see cref="LoggerProviderCollection"/> registered in the Serilog pipeline using the
/// <c>WriteTo.Providers()</c> configuration method, enabling other <see cref="Microsoft.Extensions.Logging.ILoggerProvider"/>s to receive events. By
/// default, only Serilog sinks will receive events.</param>
/// <returns>The host builder.</returns>
public static ILoggingBuilder AddSerilog(
this ILoggingBuilder builder,
Serilog.ILogger logger = null,
bool dispose = false,
LoggerProviderCollection providers = null)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));

var services = builder.Services;
if (providers != null)
{
services.AddSingleton<ILoggerFactory>(services =>
{
var factory = new SerilogLoggerFactory(logger, dispose, providers);

foreach (var provider in services.GetServices<ILoggerProvider>())
factory.AddProvider(provider);

return factory;
});
}
else
{
services.AddSingleton<ILoggerFactory>(services => new SerilogLoggerFactory(logger, dispose));
}

if (logger != null)
{
// This won't (and shouldn't) take ownership of the logger.
services.AddSingleton(logger);

// Still need to use RegisteredLogger as it is used by ConfigureDiagnosticContext.
services.AddSingleton(new RegisteredLogger(logger));
}
bool useRegisteredLogger = logger != null;
ConfigureDiagnosticContext(services, useRegisteredLogger);

return builder;
}
}
}

0 comments on commit 62bb7db

Please sign in to comment.