From f37c2874ea540e389b7418475d826f744bb0536f Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Tue, 8 Nov 2022 09:18:39 +0100 Subject: [PATCH 1/2] feat: add 'AddSerilog' extension overload with 'IServiceProvider' --- ...y.Serilog.Sinks.ApplicationInsights.csproj | 2 + .../Extensions/ILoggingBuilderExtensions.cs | 36 +++++++++++++ .../Serilog/ILoggingBuilderExtensionsTests.cs | 51 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs create mode 100644 src/Arcus.Observability.Tests.Unit/Serilog/ILoggingBuilderExtensionsTests.cs diff --git a/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.csproj b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.csproj index 429136ba..bb4a1e1d 100644 --- a/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.csproj +++ b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights.csproj @@ -25,6 +25,8 @@ + + diff --git a/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs new file mode 100644 index 00000000..1d9ea8e5 --- /dev/null +++ b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs @@ -0,0 +1,36 @@ +using System; +using GuardNet; +using Microsoft.Extensions.DependencyInjection; +using Serilog.Extensions.Logging; + +// ReSharper disable once CheckNamespace +namespace Microsoft.Extensions.Logging +{ + /// + /// Extensions on the + /// + // ReSharper disable once InconsistentNaming + public static class ILoggingBuilderExtensions + { + /// + /// Add Serilog to the logging pipeline. + /// + /// The to add logging provider to. + /// The factory function to create an Serilog logger implementation. + /// Thrown when the or is null. + public static ILoggingBuilder AddSerilog( + this ILoggingBuilder builder, + Func implementationFactory) + { + Guard.NotNull(builder, nameof(builder), "Requires a logging builder instance to add the Serilog logger provider"); + Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires an implementation factory to built up the Serilog logger"); + + builder.Services.AddSingleton(provider => + { + return new SerilogLoggerProvider(implementationFactory(provider), dispose: true); + }); + + return builder; + } + } +} diff --git a/src/Arcus.Observability.Tests.Unit/Serilog/ILoggingBuilderExtensionsTests.cs b/src/Arcus.Observability.Tests.Unit/Serilog/ILoggingBuilderExtensionsTests.cs new file mode 100644 index 00000000..6c961a19 --- /dev/null +++ b/src/Arcus.Observability.Tests.Unit/Serilog/ILoggingBuilderExtensionsTests.cs @@ -0,0 +1,51 @@ +using System; +using Arcus.Observability.Correlation; +using Arcus.Observability.Tests.Unit.Telemetry.AzureFunctions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Extensions.Logging; +using Xunit; + +namespace Arcus.Observability.Tests.Unit.Serilog +{ + // ReSharper disable once InconsistentNaming + public class ILoggingBuilderExtensionsTests + { + [Fact] + public void AddSerilog_WithImplementationFactory_IncludesSerilogLogger() + { + // Arrange + var services = new ServiceCollection(); + services.AddCorrelation(); + + // Act + services.AddLogging(builder => builder.AddSerilog(provider => + { + var accessor = provider.GetRequiredService(); + return new LoggerConfiguration() + .Enrich.WithCorrelationInfo(accessor) + .WriteTo.Console() + .CreateLogger(); + })); + + // Assert + IServiceProvider serviceProvider = services.BuildServiceProvider(); + var loggerProvider = serviceProvider.GetService(); + Assert.NotNull(loggerProvider); + Assert.IsType(loggerProvider); + Assert.NotNull(serviceProvider.GetService>()); + } + + [Fact] + public void AddSerilog_WithoutImplementationFactory_Fails() + { + // Arrange + var services = new ServiceCollection(); + + // Act / Assert + Assert.ThrowsAny( + () => services.AddLogging(builder => builder.AddSerilog(implementationFactory: null))); + } + } +} From f0dca68eee7c48e9fb7fa88bfcecf679710a8ba0 Mon Sep 17 00:00:00 2001 From: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com> Date: Fri, 11 Nov 2022 06:30:14 +0100 Subject: [PATCH 2/2] Update src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs Co-authored-by: Frederik Gheysels --- .../Extensions/ILoggingBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs index 1d9ea8e5..0c83be41 100644 --- a/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs +++ b/src/Arcus.Observability.Telemetry.Serilog.Sinks.ApplicationInsights/Extensions/ILoggingBuilderExtensions.cs @@ -23,7 +23,7 @@ public static ILoggingBuilder AddSerilog( Func implementationFactory) { Guard.NotNull(builder, nameof(builder), "Requires a logging builder instance to add the Serilog logger provider"); - Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires an implementation factory to built up the Serilog logger"); + Guard.NotNull(implementationFactory, nameof(implementationFactory), "Requires an implementation factory to build up the Serilog logger"); builder.Services.AddSingleton(provider => {