Skip to content

Commit

Permalink
Remove legacy code from LogBuilderExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Nov 20, 2024
1 parent 7e55b4f commit 5c4e553
Showing 1 changed file with 1 addition and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,11 @@ namespace OpenTelemetry.AutoInstrumentation.Logger;
internal static class LogBuilderExtensions
{
private static Type? _loggingProviderSdkType;
private static volatile bool _hostingStartupRan;

// this method is only called from LoggingBuilderIntegration
public static void AddOpenTelemetryLogsFromIntegration(ILoggingBuilder builder)
{
// For Net6, if HostingStartupAssembly is configured, we don't want to call integration again for host's ServiceCollection.
// OpenTelemetryLogger-related services were already added to WebApplicationServiceCollection and will
// be copied to host's ServiceCollection later. We can't depend on integration's
// capability to detect if integration was called before (by checking if ServiceDescriptor with
// given type is already added to ServiceCollection) as copying services from WebApplicationServiceCollection
// to host's ServiceCollection happens AFTER integration is called.

// All of this additional checking is NOT needed for net7. There we can rely on integration's capability
// to detect if integration was called before, because WebApplicationServiceCollection is not used when building host.

if (builder.Services is ServiceCollection && !(IsNet6() && _hostingStartupRan && IsHostServiceCollection(builder.Services)))
if (builder.Services is ServiceCollection)
{
AddOpenTelemetryLogs(builder);
}
Expand All @@ -38,7 +27,6 @@ public static void AddOpenTelemetryLogsFromIntegration(ILoggingBuilder builder)
public static void AddOpenTelemetryLogsFromStartup(this ILoggingBuilder builder)
{
AddOpenTelemetryLogs(builder);
_hostingStartupRan = true;
}

private static void AddOpenTelemetryLogs(ILoggingBuilder builder)
Expand Down Expand Up @@ -106,23 +94,5 @@ private static void AddOpenTelemetryLogs(ILoggingBuilder builder)
throw;
}
}

private static bool IsNet6()
{
var frameworkDescription = FrameworkDescription.Instance;
return frameworkDescription.Name == ".NET" && frameworkDescription.ProductVersion.StartsWith("6");
}

private static bool IsHostServiceCollection(IServiceCollection builderServices)
{
var applicationLifetimeType = Type.GetType("Microsoft.Extensions.Hosting.Internal.ApplicationLifetime, Microsoft.Extensions.Hosting");
if (applicationLifetimeType == null)
{
return false;
}

var applicationLifetimeDescriptor = builderServices.FirstOrDefault(sd => sd.ImplementationType == applicationLifetimeType);
return applicationLifetimeDescriptor != null;
}
}
#endif

0 comments on commit 5c4e553

Please sign in to comment.