Skip to content

Commit

Permalink
fixed an error where the logging providers would not all be resolved (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll authored Apr 30, 2024
1 parent a1f25e2 commit 44c11ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/Hosting/Conventions/HostingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,18 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Resources;
using Rocket.Surgery.Conventions;
using Rocket.Surgery.Conventions.DependencyInjection;
using Rocket.Surgery.Hosting;
using Rocket.Surgery.LaunchPad.Serilog;
using Rocket.Surgery.LaunchPad.Telemetry;

namespace Rocket.Surgery.LaunchPad.Hosting.Conventions;

[ExportConvention]
internal class HostingConvention : IServiceConvention, IHostApplicationConvention, IOpenTelemetryConvention
[AfterConvention<SerilogHostingConvention>]
internal class HostingConvention : IServiceConvention, IOpenTelemetryConvention
{
public void Register(IConventionContext context, IHostApplicationBuilder builder)
{
if (context.GetOrAdd(() => new LaunchPadLoggingOptions()).WriteToProviders != true)
{
var providers = builder.Services.Where(z => z.ServiceType == typeof(ILoggerProvider)).ToArray();
builder.Logging.ClearProviders();
builder.OnHostStarting(
provider => providers.Aggregate(
provider.GetRequiredService<ILoggerFactory>(),
(factory, descriptor) =>
{
switch (descriptor)
{
case { ImplementationFactory: { } method, } when method(provider) is ILoggerProvider p:
factory.AddProvider(p);
break;
case { ImplementationInstance: ILoggerProvider instance, }:
factory.AddProvider(instance);
break;
case { ImplementationType: { } type, } when ActivatorUtilities.CreateInstance(provider, type) is ILoggerProvider instance:
factory.AddProvider(instance);
break;
}

return factory;
}
)
);
}
}

public void Register(IConventionContext context, IConfiguration configuration, IOpenTelemetryBuilder builder)
{
builder.ConfigureResource(
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Conventions/SerilogConsoleLoggingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Rocket.Surgery.LaunchPad.Hosting.Conventions;
/// <seealso cref="ISerilogConvention" />
[PublicAPI]
[ExportConvention]
[AfterConvention<SerilogHostingConvention>]
public sealed class SerilogConsoleLoggingConvention : ISerilogConvention
{
private readonly LaunchPadLoggingOptions _options;
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Conventions/SerilogDebugLoggingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Rocket.Surgery.LaunchPad.Hosting.Conventions;
/// <seealso cref="ISerilogConvention" />
[PublicAPI]
[ExportConvention]
[AfterConvention<SerilogHostingConvention>]
public sealed class SerilogDebugLoggingConvention : ISerilogConvention
{
private readonly LaunchPadLoggingOptions _options;
Expand Down
15 changes: 15 additions & 0 deletions src/Hosting/Conventions/SerilogHostingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,20 @@ public void Register(IConventionContext context, IHostApplicationBuilder builder
// ReSharper disable once NullableWarningSuppressionIsUsed
builder.Services.AddSingleton(context.Get<ILoggerFactory>()!);
}

if (_options.WriteToProviders) return;

builder.OnHostStarting(
provider => provider
.GetServices<ILoggerProvider>()
.Aggregate(
provider.GetRequiredService<ILoggerFactory>(),
(factory, loggerProvider) =>
{
factory.AddProvider(loggerProvider);
return factory;
}
)
);
}
}

0 comments on commit 44c11ee

Please sign in to comment.