From 07501bf2964b883ffb53dea4c3093f629ad8c972 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Thu, 23 May 2024 14:44:15 -0700 Subject: [PATCH] Use WithLogging in example app. --- examples/AspNetCore/Program.cs | 52 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/examples/AspNetCore/Program.cs b/examples/AspNetCore/Program.cs index 06b82c3df43..7d6ced849f8 100644 --- a/examples/AspNetCore/Program.cs +++ b/examples/AspNetCore/Program.cs @@ -34,7 +34,10 @@ // for manual instrumentation appBuilder.Services.AddSingleton(); -// Configure OpenTelemetry tracing & metrics with auto-start using the +// Clear default logging providers used by WebApplication host. +appBuilder.Logging.ClearProviders(); + +// Configure OpenTelemetry logging, metrics, & tracing with auto-start using the // AddOpenTelemetry extension from OpenTelemetry.Extensions.Hosting. appBuilder.Services.AddOpenTelemetry() .ConfigureResource(configureResource) @@ -121,34 +124,29 @@ builder.AddConsoleExporter(); break; } - }); - -// Clear default logging providers used by WebApplication host. -appBuilder.Logging.ClearProviders(); - -// Configure OpenTelemetry Logging. -appBuilder.Logging.UseOpenTelemetry(builder => -{ - // Note: See appsettings.json Logging:OpenTelemetry section for configuration. + }) + .WithLogging(builder => + { + // Note: See appsettings.json Logging:OpenTelemetry section for configuration. - var resourceBuilder = ResourceBuilder.CreateDefault(); - configureResource(resourceBuilder); - builder.SetResourceBuilder(resourceBuilder); + var resourceBuilder = ResourceBuilder.CreateDefault(); + configureResource(resourceBuilder); + builder.SetResourceBuilder(resourceBuilder); - switch (logExporter) - { - case "otlp": - builder.AddOtlpExporter(otlpOptions => - { - // Use IConfiguration directly for Otlp exporter endpoint option. - otlpOptions.Endpoint = new Uri(appBuilder.Configuration.GetValue("Otlp:Endpoint", defaultValue: "http://localhost:4317")!); - }); - break; - default: - builder.AddConsoleExporter(); - break; - } -}); + switch (logExporter) + { + case "otlp": + builder.AddOtlpExporter(otlpOptions => + { + // Use IConfiguration directly for Otlp exporter endpoint option. + otlpOptions.Endpoint = new Uri(appBuilder.Configuration.GetValue("Otlp:Endpoint", defaultValue: "http://localhost:4317")!); + }); + break; + default: + builder.AddConsoleExporter(); + break; + } + }); appBuilder.Services.AddControllers();