Skip to content

Commit

Permalink
Added a Serilog extension to work with the new HostApplicationBuilder…
Browse files Browse the repository at this point in the history
… pattern.
  • Loading branch information
jonsagara committed Sep 1, 2023
1 parent fd7350a commit 2201fc0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>

<!-- NuGet -->
<Version>1.0.0-alpha4</Version>
<Version>1.0.0-alpha5</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Authors>Jon Sagara</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
42 changes: 42 additions & 0 deletions src/Sagara.Core.Logging.Serilog/SerilogExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Serilog;

namespace Sagara.Core.Logging.Serilog;

public static class SerilogExtensions
{
/// <summary>
/// Sets Serilog as the logging provider.
/// </summary>
/// <remarks>
/// <para>This is (hopefully) a temporary replacement for <c>Serilog.Extensions.Hosting.UseSerilog</c>. It's necessary
/// to support working with the new <c>Host.CreateApplicationBuilder()</c> pattern being rolled out in .NET 7, which
/// does not currently support the <c>IHostBuilder</c> interface.</para>
/// <para>See: https://github.com/dotnet/runtime/discussions/81090#discussioncomment-4784551</para>
/// </remarks>
/// <param name="builder">The logging builder to configure.</param>
/// <param name="config">The IConfiguration from which Serilog will attempt to read its configuration.</param>
/// <param name="configureLogger">The delegate for configuring the Serilog.LoggerConfiguration that will be used
/// to construct a Serilog.Core.Logger.</param>
/// <param name="preserveStaticLogger">Indicates whether to preserve the value of Serilog.Log.Logger.</param>
/// <param name="writeToProviders">By default, Serilog does not write events to Microsoft.Extensions.Logging.ILoggerProviders
/// 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.</param>
/// <returns>The logging builder.</returns>
public static ILoggingBuilder UseSerilog(this ILoggingBuilder builder, IConfiguration config, Action<IConfiguration, IServiceProvider, LoggerConfiguration> configureLogger, bool preserveStaticLogger = false, bool writeToProviders = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configureLogger);

IConfiguration config2 = config;

builder.Services.AddSerilog(
(serviceProvider, loggerConfiguration) => configureLogger(config2, serviceProvider, loggerConfiguration),
preserveStaticLogger: preserveStaticLogger,
writeToProviders: writeToProviders
);

return builder;
}
}

0 comments on commit 2201fc0

Please sign in to comment.