diff --git a/docs/README.md b/docs/README.md index 23d80c8a577..7818bda1be4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -93,6 +93,11 @@ var sdk = OpenTelemetrySdk.Create(builder => builder .WithMetrics(metrics => /* Metrics configuration goes here */) .WithTracing(tracing => /* Tracing configuration goes here */)); +// Optionally ForceFlush() telemetry objects in memory +sdk.LoggerProvider.ForceFlush(); +sdk.MeterProvider.ForceFlush(); +sdk.TracerProvider.ForceFlush(); + // During application shutdown sdk.Dispose(); ``` diff --git a/docs/logs/complex-objects/Program.cs b/docs/logs/complex-objects/Program.cs index c09cfc5c772..8e9624d3095 100644 --- a/docs/logs/complex-objects/Program.cs +++ b/docs/logs/complex-objects/Program.cs @@ -2,17 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); -var logger = loggerFactory.CreateLogger(); +var logger = sdk.GetLoggerFactory().CreateLogger(); var foodRecallNotice = new FoodRecallNotice { @@ -25,9 +21,8 @@ logger.FoodRecallNotice(foodRecallNotice); -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); +// Dispose SDK before the application ends. +sdk.Dispose(); internal static partial class LoggerExtensions { diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index b1a3284fbcc..27e0deffad8 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -13,20 +13,15 @@ public class Program public static void Main() { - var tracerProvider = Sdk.CreateTracerProviderBuilder() - .AddSource("MyCompany.MyProduct.MyLibrary") - .AddConsoleExporter() - .Build(); - - var loggerFactory = LoggerFactory.Create(builder => - { - builder.AddOpenTelemetry(logging => + var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter()) + .WithTracing(tracing => { - logging.AddConsoleExporter(); - }); - }); + tracing.AddSource("MyCompany.MyProduct.MyLibrary"); + tracing.AddConsoleExporter(); + })); - var logger = loggerFactory.CreateLogger(); + var logger = sdk.GetLoggerFactory().CreateLogger(); using (var activity = MyActivitySource.StartActivity("SayHello")) { @@ -34,12 +29,7 @@ public static void Main() logger.FoodPriceChanged("artichoke", 9.99); } - // Dispose logger factory before the application ends. - // This will flush the remaining logs and shutdown the logging pipeline. - loggerFactory.Dispose(); - - // Dispose tracer provider before the application ends. - // This will flush the remaining spans and shutdown the tracing pipeline. - tracerProvider.Dispose(); + // Dispose SDK before the application ends. + sdk.Dispose(); } } diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index 1b22fdbfda9..b576dcd91f2 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -2,22 +2,22 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; using OpenTelemetry.Resources; -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.IncludeScopes = true; - logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( - serviceName: "MyService", - serviceVersion: "1.0.0")); - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging( + logging => + { + logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( + serviceName: "MyService", + serviceVersion: "1.0.0")); + logging.AddConsoleExporter(); + }, + options => options.IncludeScopes = true)); + +var logger = sdk.GetLoggerFactory().CreateLogger(); logger.FoodPriceChanged("artichoke", 9.99); @@ -36,9 +36,8 @@ recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", companyName: "Contoso Fresh Vegetables, Inc."); -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); +// Dispose SDK before the application ends. +sdk.Dispose(); internal static partial class LoggerExtensions { diff --git a/docs/logs/extending-the-sdk/LoggerExtensions.cs b/docs/logs/extending-the-sdk/LoggerExtensions.cs index 7476d833334..bff982b55d7 100644 --- a/docs/logs/extending-the-sdk/LoggerExtensions.cs +++ b/docs/logs/extending-the-sdk/LoggerExtensions.cs @@ -6,7 +6,7 @@ internal static class LoggerExtensions { - public static OpenTelemetryLoggerOptions AddMyExporter(this OpenTelemetryLoggerOptions options) + public static LoggerProviderBuilder AddMyExporter(this LoggerProviderBuilder options) { if (options == null) { diff --git a/docs/logs/extending-the-sdk/Program.cs b/docs/logs/extending-the-sdk/Program.cs index cada6ec1f5d..1fd745ec2be 100644 --- a/docs/logs/extending-the-sdk/Program.cs +++ b/docs/logs/extending-the-sdk/Program.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Logging; using OpenTelemetry; +using OpenTelemetry.Logs; namespace ExtendingTheSdk; @@ -10,17 +11,18 @@ public class Program { public static void Main() { - using var loggerFactory = LoggerFactory.Create(builder => - builder.AddOpenTelemetry(options => - { - options.IncludeScopes = true; - options.AddProcessor(new MyProcessor("ProcessorA")) - .AddProcessor(new MyProcessor("ProcessorB")) - .AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX"))) - .AddMyExporter(); - })); - - var logger = loggerFactory.CreateLogger(); + var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging( + logging => + { + logging.AddProcessor(new MyProcessor("ProcessorA")); + logging.AddProcessor(new MyProcessor("ProcessorB")); + logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX"))); + logging.AddMyExporter(); + }, + options => options.IncludeScopes = true)); + + var logger = sdk.GetLoggerFactory().CreateLogger(); // unstructured log logger.LogInformation("Hello, World!"); @@ -53,6 +55,13 @@ public static void Main() // message will be redacted by MyRedactionProcessor logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); + + // Attempt to flush the remaining logs when using an exporter that may require it, such as the custom one defined here. + // Ignoring success or failure for flush after 5 seconds, but a richer application should choose to handle this in some manner. + sdk.LoggerProvider.ForceFlush(5000); + + // Dispose SDK before the application ends. + sdk.Dispose(); } internal struct Food diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index b907d169d20..ba366c74e86 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -2,17 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); -var logger = loggerFactory.CreateLogger(); +var logger = sdk.GetLoggerFactory().CreateLogger(); logger.FoodPriceChanged("artichoke", 9.99); @@ -23,9 +19,8 @@ recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", companyName: "Contoso Fresh Vegetables, Inc."); -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); +// Dispose SDK before the application ends. +sdk.Dispose(); internal static partial class LoggerExtensions { diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index fa33d884581..af336a00c82 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -2,25 +2,23 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => { logging.AddProcessor(new MyRedactionProcessor()); logging.AddConsoleExporter(); - }); -}); + })); -var logger = loggerFactory.CreateLogger(); +var logger = sdk.GetLoggerFactory().CreateLogger(); // Message will be redacted by MyRedactionProcessor logger.FoodPriceChanged("", 9.99); -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); +// Dispose SDK before the application ends. +sdk.Dispose(); internal static partial class LoggerExtensions {