From 1cfe0299ac340c58985ab0ca75768e9901fc018e Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 5 Dec 2024 15:30:16 -0800 Subject: [PATCH 01/10] doc update --- docs/logs/complex-objects/Program.cs | 13 +++++++++---- docs/logs/correlation/Program.cs | 7 ++++++- docs/logs/customizing-the-sdk/Program.cs | 15 ++++++++++++--- docs/logs/dedicated-pipeline/Program.cs | 2 ++ docs/logs/extending-the-sdk/Program.cs | 11 ++++++++++- docs/logs/getting-started-aspnetcore/Program.cs | 3 +++ docs/logs/getting-started-console/Program.cs | 14 ++++++++++++-- docs/logs/redaction/Program.cs | 8 +++++--- 8 files changed, 59 insertions(+), 14 deletions(-) diff --git a/docs/logs/complex-objects/Program.cs b/docs/logs/complex-objects/Program.cs index c09cfc5c772..83d70ac765a 100644 --- a/docs/logs/complex-objects/Program.cs +++ b/docs/logs/complex-objects/Program.cs @@ -2,8 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(logging => @@ -23,10 +26,12 @@ CompanyName = "Contoso Fresh Vegetables, Inc.", }; -logger.FoodRecallNotice(foodRecallNotice); - -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. +logger.FoodRecallNotice(foodRecallNotice); + +// This will flush the remaining logs. +loggerProvider.ForceFlush(); + +// This will shutdown the logging pipeline. loggerFactory.Dispose(); internal static partial class LoggerExtensions diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index b1a3284fbcc..f8a37ffd47e 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -13,6 +13,8 @@ public class Program public static void Main() { + var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyLibrary") .AddConsoleExporter() @@ -34,8 +36,11 @@ public static void Main() logger.FoodPriceChanged("artichoke", 9.99); } + // This will flush the remaining logs. + loggerProvider.ForceFlush(); + // Dispose logger factory before the application ends. - // This will flush the remaining logs and shutdown the logging pipeline. + // This will shutdown the logging pipeline. loggerFactory.Dispose(); // Dispose tracer provider before the application ends. diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index 1b22fdbfda9..a2d8b50c4ff 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -2,9 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; using OpenTelemetry.Resources; +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(logging => @@ -12,7 +15,11 @@ logging.IncludeScopes = true; logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( serviceName: "MyService", - serviceVersion: "1.0.0")); + serviceVersion: "1.0.0") + .AddAttributes(new Dictionary + { + { "service.instance.id", "my-instance" }, + })); logging.AddConsoleExporter(); }); }); @@ -36,8 +43,10 @@ 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. +// This will flush the remaining logs. +loggerProvider.ForceFlush(); + +// This will shutdown the logging pipeline. loggerFactory.Dispose(); internal static partial class LoggerExtensions diff --git a/docs/logs/dedicated-pipeline/Program.cs b/docs/logs/dedicated-pipeline/Program.cs index c465c77d0d3..e89db10a58a 100644 --- a/docs/logs/dedicated-pipeline/Program.cs +++ b/docs/logs/dedicated-pipeline/Program.cs @@ -38,6 +38,8 @@ app.Run(); +// Any flush or shutdown operations we want to showcase? + internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] diff --git a/docs/logs/extending-the-sdk/Program.cs b/docs/logs/extending-the-sdk/Program.cs index cada6ec1f5d..72c5fc2f733 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,6 +11,8 @@ public class Program { public static void Main() { + var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + using var loggerFactory = LoggerFactory.Create(builder => builder.AddOpenTelemetry(options => { @@ -52,7 +55,13 @@ public static void Main() } // message will be redacted by MyRedactionProcessor - logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); + logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); + + // This will flush the remaining logs. + loggerProvider.ForceFlush(); + + // This will shutdown the logging pipeline. + loggerFactory.Dispose(); } internal struct Food diff --git a/docs/logs/getting-started-aspnetcore/Program.cs b/docs/logs/getting-started-aspnetcore/Program.cs index 123bf54a2c9..9eba24c217c 100644 --- a/docs/logs/getting-started-aspnetcore/Program.cs +++ b/docs/logs/getting-started-aspnetcore/Program.cs @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; using OpenTelemetry.Resources; @@ -35,6 +36,8 @@ app.Run(); +// Any flush or shutdown operations we want to showcase? + internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Starting the app...")] diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index b907d169d20..332d8a1a73d 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -2,8 +2,16 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; +// Comment for PR to think about: +// -Unclear to me if this needs to be before LoggerFactory.Create and if they interact at all. +// -Unintituive that this `LoggerProvider` is not an `ILoggerProvider`. +// -In general, I think the `Sdk.Create*()` APIs are going to add confusion with the existing .Net blogs posts of +// how to use ILogger, ILoggerProvider, and ILoggerFactory. +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(logging => @@ -23,8 +31,10 @@ 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. +// This will flush the remaining logs. +loggerProvider.ForceFlush(); + +// This will shutdown the logging pipeline. loggerFactory.Dispose(); internal static partial class LoggerExtensions diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index fa33d884581..cbfec014e37 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -2,8 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 using Microsoft.Extensions.Logging; +using OpenTelemetry; using OpenTelemetry.Logs; +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(logging => @@ -18,9 +21,8 @@ // 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(); +// This will flush the remaining logs. +loggerProvider.ForceFlush(); internal static partial class LoggerExtensions { From ab2f2fca7799b9411d26387977650ecdf71ef2dc Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 5 Dec 2024 15:48:08 -0800 Subject: [PATCH 02/10] doc fixes --- docs/logs/correlation/Program.cs | 1 - docs/logs/customizing-the-sdk/Program.cs | 120 +++++++++--------- .../getting-started-aspnetcore/Program.cs | 1 - docs/logs/redaction/Program.cs | 3 + 4 files changed, 61 insertions(+), 64 deletions(-) diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index f8a37ffd47e..fa976e0ab7d 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -39,7 +39,6 @@ public static void Main() // This will flush the remaining logs. loggerProvider.ForceFlush(); - // Dispose logger factory before the application ends. // This will shutdown the logging pipeline. loggerFactory.Dispose(); diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index a2d8b50c4ff..e8225c0591a 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -1,65 +1,61 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; -using OpenTelemetry; -using OpenTelemetry.Logs; -using OpenTelemetry.Resources; - -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.IncludeScopes = true; - logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( - serviceName: "MyService", - serviceVersion: "1.0.0") - .AddAttributes(new Dictionary - { - { "service.instance.id", "my-instance" }, - })); - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); - -logger.FoodPriceChanged("artichoke", 9.99); - -using (logger.BeginScope(new List> -{ - new KeyValuePair("store", "Seattle"), -})) -{ - logger.FoodPriceChanged("truffle", 999.99); -} - -logger.FoodRecallNotice( - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - -// This will flush the remaining logs. +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Logs; +using OpenTelemetry.Resources; + +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + +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(); + +logger.FoodPriceChanged("artichoke", 9.99); + +using (logger.BeginScope(new List> +{ + new KeyValuePair("store", "Seattle"), +})) +{ + logger.FoodPriceChanged("truffle", 999.99); +} + +logger.FoodRecallNotice( + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + +// This will flush the remaining logs. loggerProvider.ForceFlush(); // This will shutdown the logging pipeline. -loggerFactory.Dispose(); - -internal static partial class LoggerExtensions -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} +loggerFactory.Dispose(); + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + + [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); +} diff --git a/docs/logs/getting-started-aspnetcore/Program.cs b/docs/logs/getting-started-aspnetcore/Program.cs index 9eba24c217c..12ab4ff36b1 100644 --- a/docs/logs/getting-started-aspnetcore/Program.cs +++ b/docs/logs/getting-started-aspnetcore/Program.cs @@ -1,7 +1,6 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; using OpenTelemetry.Resources; diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index cbfec014e37..3d69a478790 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -24,6 +24,9 @@ // This will flush the remaining logs. loggerProvider.ForceFlush(); +// This will shutdown the logging pipeline. +loggerFactory.Dispose(); + internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] From 1638ee81f6988e4a9dc98c22b0445f9e4efbb41a Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 5 Dec 2024 15:55:13 -0800 Subject: [PATCH 03/10] CRLF and few comments --- .../getting-started-aspnetcore/Program.cs | 2 - docs/logs/getting-started-console/Program.cs | 95 +++++++++---------- 2 files changed, 45 insertions(+), 52 deletions(-) diff --git a/docs/logs/getting-started-aspnetcore/Program.cs b/docs/logs/getting-started-aspnetcore/Program.cs index 12ab4ff36b1..123bf54a2c9 100644 --- a/docs/logs/getting-started-aspnetcore/Program.cs +++ b/docs/logs/getting-started-aspnetcore/Program.cs @@ -35,8 +35,6 @@ app.Run(); -// Any flush or shutdown operations we want to showcase? - internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Starting the app...")] diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 332d8a1a73d..279411935a7 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -1,53 +1,48 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; -using OpenTelemetry; -using OpenTelemetry.Logs; - -// Comment for PR to think about: -// -Unclear to me if this needs to be before LoggerFactory.Create and if they interact at all. -// -Unintituive that this `LoggerProvider` is not an `ILoggerProvider`. -// -In general, I think the `Sdk.Create*()` APIs are going to add confusion with the existing .Net blogs posts of -// how to use ILogger, ILoggerProvider, and ILoggerFactory. -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); - -logger.FoodPriceChanged("artichoke", 9.99); - -logger.FoodRecallNotice( - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - -// This will flush the remaining logs. +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Logs; + +var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); + +var loggerFactory = LoggerFactory.Create(builder => +{ + builder.AddOpenTelemetry(logging => + { + logging.AddConsoleExporter(); + }); +}); + +var logger = loggerFactory.CreateLogger(); + +logger.FoodPriceChanged("artichoke", 9.99); + +logger.FoodRecallNotice( + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + +// This will flush the remaining logs. loggerProvider.ForceFlush(); // This will shutdown the logging pipeline. -loggerFactory.Dispose(); - -internal static partial class LoggerExtensions -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} +loggerFactory.Dispose(); + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + + [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); +} From 6196af370b2be4b0e8db9e9801e68f0ba1a7196b Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 5 Dec 2024 15:56:45 -0800 Subject: [PATCH 04/10] minor removal --- docs/logs/dedicated-pipeline/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/logs/dedicated-pipeline/Program.cs b/docs/logs/dedicated-pipeline/Program.cs index e89db10a58a..c465c77d0d3 100644 --- a/docs/logs/dedicated-pipeline/Program.cs +++ b/docs/logs/dedicated-pipeline/Program.cs @@ -38,8 +38,6 @@ app.Run(); -// Any flush or shutdown operations we want to showcase? - internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] From 4fd23ec237109445523fd390281df373e6098e33 Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 10:17:55 -0800 Subject: [PATCH 05/10] Add di console example, switch loggerfactory usage to sdk logger factory --- OpenTelemetry.sln | 7 ++++ docs/README.md | 4 ++ docs/logs/complex-objects/Program.cs | 21 ++++------ docs/logs/correlation/Program.cs | 32 ++++++--------- docs/logs/customizing-the-sdk/Program.cs | 20 ++++------ .../extending-the-sdk/LoggerExtensions.cs | 2 +- docs/logs/extending-the-sdk/Program.cs | 30 +++++++------- .../ExampleService.cs | 19 +++++++++ .../getting-started-console-di/Program.cs | 39 +++++++++++++++++++ .../logs/getting-started-console-di/README.md | 3 ++ .../getting-started-console-di.csproj | 6 +++ docs/logs/getting-started-console/Program.cs | 19 +++------ docs/logs/redaction/Program.cs | 20 +++------- 13 files changed, 132 insertions(+), 90 deletions(-) create mode 100644 docs/logs/getting-started-console-di/ExampleService.cs create mode 100644 docs/logs/getting-started-console-di/Program.cs create mode 100644 docs/logs/getting-started-console-di/README.md create mode 100644 docs/logs/getting-started-console-di/getting-started-console-di.csproj diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln index c109bf10753..df2307c5ddb 100644 --- a/OpenTelemetry.sln +++ b/OpenTelemetry.sln @@ -350,6 +350,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Proto", "Proto", "{02EA681E src\Shared\Proto\README.md = src\Shared\Proto\README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "getting-started-console-di", "docs\logs\getting-started-console-di\getting-started-console-di.csproj", "{6D6F0582-4AA1-42B3-887F-415375263F0E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -608,6 +610,10 @@ Global {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Debug|Any CPU.Build.0 = Debug|Any CPU {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Release|Any CPU.ActiveCfg = Release|Any CPU {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Release|Any CPU.Build.0 = Release|Any CPU + {6D6F0582-4AA1-42B3-887F-415375263F0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D6F0582-4AA1-42B3-887F-415375263F0E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D6F0582-4AA1-42B3-887F-415375263F0E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D6F0582-4AA1-42B3-887F-415375263F0E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -667,6 +673,7 @@ Global {44982E0D-C8C6-42DC-9F8F-714981F27CE6} = {7CB2F02E-03FA-4FFF-89A5-C51F107623FD} {79C12C80-B27B-41FB-AE79-A3BB74CFA782} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB} {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC} + {6D6F0582-4AA1-42B3-887F-415375263F0E} = {3862190B-E2C5-418E-AFDC-DB281FB5C705} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521} diff --git a/docs/README.md b/docs/README.md index 23d80c8a577..c2371f2449f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -93,6 +93,10 @@ 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. + // During application shutdown sdk.Dispose(); ``` diff --git a/docs/logs/complex-objects/Program.cs b/docs/logs/complex-objects/Program.cs index 83d70ac765a..197969c9475 100644 --- a/docs/logs/complex-objects/Program.cs +++ b/docs/logs/complex-objects/Program.cs @@ -5,17 +5,10 @@ using OpenTelemetry; using OpenTelemetry.Logs; -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); + +var logger = sdk.GetLoggerFactory().CreateLogger(); var foodRecallNotice = new FoodRecallNotice { @@ -29,10 +22,10 @@ logger.FoodRecallNotice(foodRecallNotice); // This will flush the remaining logs. -loggerProvider.ForceFlush(); +sdk.LoggerProvider.ForceFlush(); -// This will 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 fa976e0ab7d..9ee9bf12caf 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -13,22 +13,15 @@ public class Program public static void Main() { - var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - - 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")) { @@ -37,13 +30,12 @@ public static void Main() } // This will flush the remaining logs. - loggerProvider.ForceFlush(); + sdk.LoggerProvider.ForceFlush(); - // This will shutdown the logging pipeline. - loggerFactory.Dispose(); + // This will flush the remaining traces. + sdk.TracerProvider.ForceFlush(); - // 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 e8225c0591a..7767e34f3ca 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -6,21 +6,17 @@ using OpenTelemetry.Logs; using OpenTelemetry.Resources; -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => { - logging.IncludeScopes = true; + // logging.IncludeScopes = true; logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( serviceName: "MyService", serviceVersion: "1.0.0")); logging.AddConsoleExporter(); - }); -}); + })); -var logger = loggerFactory.CreateLogger(); +var logger = sdk.GetLoggerFactory().CreateLogger(); logger.FoodPriceChanged("artichoke", 9.99); @@ -40,10 +36,10 @@ companyName: "Contoso Fresh Vegetables, Inc."); // This will flush the remaining logs. -loggerProvider.ForceFlush(); +sdk.LoggerProvider.ForceFlush(); -// This will 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 72c5fc2f733..ce92b32a874 100644 --- a/docs/logs/extending-the-sdk/Program.cs +++ b/docs/logs/extending-the-sdk/Program.cs @@ -11,19 +11,17 @@ public class Program { public static void Main() { - var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); - - 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.IncludeScopes = true; + logging.AddProcessor(new MyProcessor("ProcessorA")); + logging.AddProcessor(new MyProcessor("ProcessorB")); + logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX"))); + logging.AddMyExporter(); + })); + + var logger = sdk.GetLoggerFactory().CreateLogger(); // unstructured log logger.LogInformation("Hello, World!"); @@ -58,10 +56,10 @@ public static void Main() logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); // This will flush the remaining logs. - loggerProvider.ForceFlush(); + sdk.LoggerProvider.ForceFlush(); - // This will shutdown the logging pipeline. - loggerFactory.Dispose(); + // Dispose SDK before the application ends. + sdk.Dispose(); } internal struct Food diff --git a/docs/logs/getting-started-console-di/ExampleService.cs b/docs/logs/getting-started-console-di/ExampleService.cs new file mode 100644 index 00000000000..5a7c497729b --- /dev/null +++ b/docs/logs/getting-started-console-di/ExampleService.cs @@ -0,0 +1,19 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; + +internal class ExampleService(ILogger logger) +{ + public void DoSomeWork() + { + logger.FoodPriceChanged("artichoke", 9.99); + + logger.FoodRecallNotice( + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + } +} diff --git a/docs/logs/getting-started-console-di/Program.cs b/docs/logs/getting-started-console-di/Program.cs new file mode 100644 index 00000000000..e8c4556c440 --- /dev/null +++ b/docs/logs/getting-started-console-di/Program.cs @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Logs; + +// Add services to the container, including OpenTelemetry with logging +var services = new ServiceCollection(); +services.AddOpenTelemetry().WithLogging(logging => logging.AddConsoleExporter()); +services.AddSingleton(); +IServiceProvider serviceProvider = services.BuildServiceProvider(); + +// Get the ExampleService object from the container +ExampleService service = serviceProvider.GetRequiredService(); + +service.DoSomeWork(); + +// This will flush the remaining logs. +serviceProvider.GetRequiredService().ForceFlush(); + +// Dispose SDK before the application ends. +serviceProvider.GetRequiredService().Dispose(); + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + + [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); +} diff --git a/docs/logs/getting-started-console-di/README.md b/docs/logs/getting-started-console-di/README.md new file mode 100644 index 00000000000..bd3c661863c --- /dev/null +++ b/docs/logs/getting-started-console-di/README.md @@ -0,0 +1,3 @@ +# Console Application with Dependency Injection + +This example shows how create a console application with Dependency Injection via `ServiceCollection`. diff --git a/docs/logs/getting-started-console-di/getting-started-console-di.csproj b/docs/logs/getting-started-console-di/getting-started-console-di.csproj new file mode 100644 index 00000000000..9e7b3138c7e --- /dev/null +++ b/docs/logs/getting-started-console-di/getting-started-console-di.csproj @@ -0,0 +1,6 @@ + + + + + + diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 279411935a7..d336daaedc4 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -5,17 +5,10 @@ using OpenTelemetry; using OpenTelemetry.Logs; -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); +var logger = sdk.GetLoggerFactory().CreateLogger(); logger.FoodPriceChanged("artichoke", 9.99); @@ -27,10 +20,10 @@ companyName: "Contoso Fresh Vegetables, Inc."); // This will flush the remaining logs. -loggerProvider.ForceFlush(); +sdk.LoggerProvider.ForceFlush(); -// This will 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 3d69a478790..07f8719cda3 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -5,27 +5,19 @@ using OpenTelemetry; using OpenTelemetry.Logs; -var loggerProvider = Sdk.CreateLoggerProviderBuilder().Build(); +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(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); // This will flush the remaining logs. -loggerProvider.ForceFlush(); +sdk.LoggerProvider.ForceFlush(); -// This will shutdown the logging pipeline. -loggerFactory.Dispose(); +// Dispose SDK before the application ends. +sdk.Dispose(); internal static partial class LoggerExtensions { From 655a8f60619b94dd6a9cb8b7161e0e3eb25a32c0 Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 10:23:18 -0800 Subject: [PATCH 06/10] Update README.md Missed some readme updates. --- docs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index c2371f2449f..7818bda1be4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -95,7 +95,8 @@ var sdk = OpenTelemetrySdk.Create(builder => builder // Optionally ForceFlush() telemetry objects in memory sdk.LoggerProvider.ForceFlush(); -sdk. +sdk.MeterProvider.ForceFlush(); +sdk.TracerProvider.ForceFlush(); // During application shutdown sdk.Dispose(); From aaecec79b67c6becc953b872e40ea76758482d4f Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 11:17:41 -0800 Subject: [PATCH 07/10] Fix include scopes, service disposal, remove flush from irrelevant examples --- docs/logs/complex-objects/Program.cs | 3 --- docs/logs/correlation/Program.cs | 6 ----- docs/logs/customizing-the-sdk/Program.cs | 20 ++++++++--------- docs/logs/extending-the-sdk/Program.cs | 22 ++++++++++--------- .../getting-started-console-di/Program.cs | 10 +++------ docs/logs/getting-started-console/Program.cs | 3 --- docs/logs/redaction/Program.cs | 3 --- 7 files changed, 24 insertions(+), 43 deletions(-) diff --git a/docs/logs/complex-objects/Program.cs b/docs/logs/complex-objects/Program.cs index 197969c9475..e44010edd01 100644 --- a/docs/logs/complex-objects/Program.cs +++ b/docs/logs/complex-objects/Program.cs @@ -21,9 +21,6 @@ logger.FoodRecallNotice(foodRecallNotice); -// This will flush the remaining logs. -sdk.LoggerProvider.ForceFlush(); - // Dispose SDK before the application ends. sdk.Dispose(); diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index 9ee9bf12caf..27e0deffad8 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -29,12 +29,6 @@ public static void Main() logger.FoodPriceChanged("artichoke", 9.99); } - // This will flush the remaining logs. - sdk.LoggerProvider.ForceFlush(); - - // This will flush the remaining traces. - sdk.TracerProvider.ForceFlush(); - // 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 7767e34f3ca..40a041a2a2d 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -7,14 +7,15 @@ using OpenTelemetry.Resources; var sdk = OpenTelemetrySdk.Create(builder => builder - .WithLogging(logging => - { - // logging.IncludeScopes = true; - logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( - serviceName: "MyService", - serviceVersion: "1.0.0")); - logging.AddConsoleExporter(); - })); + .WithLogging( + logging => + { + logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( + serviceName: "MyService", + serviceVersion: "1.0.0")); + logging.AddConsoleExporter(); + }, + options => options.IncludeScopes = true)); var logger = sdk.GetLoggerFactory().CreateLogger(); @@ -35,9 +36,6 @@ recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", companyName: "Contoso Fresh Vegetables, Inc."); -// This will flush the remaining logs. -sdk.LoggerProvider.ForceFlush(); - // Dispose SDK before the application ends. sdk.Dispose(); diff --git a/docs/logs/extending-the-sdk/Program.cs b/docs/logs/extending-the-sdk/Program.cs index ce92b32a874..cf72e2e2f49 100644 --- a/docs/logs/extending-the-sdk/Program.cs +++ b/docs/logs/extending-the-sdk/Program.cs @@ -12,14 +12,15 @@ public class Program public static void Main() { var sdk = OpenTelemetrySdk.Create(builder => builder - .WithLogging(logging => - { - // logging.IncludeScopes = true; - logging.AddProcessor(new MyProcessor("ProcessorA")); - logging.AddProcessor(new MyProcessor("ProcessorB")); - logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX"))); - logging.AddMyExporter(); - })); + .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(); @@ -55,8 +56,9 @@ public static void Main() // message will be redacted by MyRedactionProcessor logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); - // This will flush the remaining logs. - sdk.LoggerProvider.ForceFlush(); + // 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(); diff --git a/docs/logs/getting-started-console-di/Program.cs b/docs/logs/getting-started-console-di/Program.cs index e8c4556c440..b1a149fe981 100644 --- a/docs/logs/getting-started-console-di/Program.cs +++ b/docs/logs/getting-started-console-di/Program.cs @@ -3,25 +3,21 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using OpenTelemetry; using OpenTelemetry.Logs; // Add services to the container, including OpenTelemetry with logging var services = new ServiceCollection(); services.AddOpenTelemetry().WithLogging(logging => logging.AddConsoleExporter()); services.AddSingleton(); -IServiceProvider serviceProvider = services.BuildServiceProvider(); +ServiceProvider serviceProvider = services.BuildServiceProvider(); // Get the ExampleService object from the container ExampleService service = serviceProvider.GetRequiredService(); service.DoSomeWork(); -// This will flush the remaining logs. -serviceProvider.GetRequiredService().ForceFlush(); - -// Dispose SDK before the application ends. -serviceProvider.GetRequiredService().Dispose(); +// Dispose before the application ends. +serviceProvider.Dispose(); internal static partial class LoggerExtensions { diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index d336daaedc4..199a33c5aa4 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -19,9 +19,6 @@ recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", companyName: "Contoso Fresh Vegetables, Inc."); -// This will flush the remaining logs. -sdk.LoggerProvider.ForceFlush(); - // Dispose SDK before the application ends. sdk.Dispose(); diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index 07f8719cda3..0b3d2fb6cc4 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -13,9 +13,6 @@ // Message will be redacted by MyRedactionProcessor logger.FoodPriceChanged("", 9.99); -// This will flush the remaining logs. -sdk.LoggerProvider.ForceFlush(); - // Dispose SDK before the application ends. sdk.Dispose(); From 0be8943c99fd2895609577a2aa56fdcc733bf96b Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 11:19:12 -0800 Subject: [PATCH 08/10] Remove new console di example project --- OpenTelemetry.sln | 7 ---- .../ExampleService.cs | 19 ---------- .../getting-started-console-di/Program.cs | 35 ------------------- .../logs/getting-started-console-di/README.md | 3 -- .../getting-started-console-di.csproj | 6 ---- 5 files changed, 70 deletions(-) delete mode 100644 docs/logs/getting-started-console-di/ExampleService.cs delete mode 100644 docs/logs/getting-started-console-di/Program.cs delete mode 100644 docs/logs/getting-started-console-di/README.md delete mode 100644 docs/logs/getting-started-console-di/getting-started-console-di.csproj diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln index df2307c5ddb..c109bf10753 100644 --- a/OpenTelemetry.sln +++ b/OpenTelemetry.sln @@ -350,8 +350,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Proto", "Proto", "{02EA681E src\Shared\Proto\README.md = src\Shared\Proto\README.md EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "getting-started-console-di", "docs\logs\getting-started-console-di\getting-started-console-di.csproj", "{6D6F0582-4AA1-42B3-887F-415375263F0E}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -610,10 +608,6 @@ Global {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Debug|Any CPU.Build.0 = Debug|Any CPU {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Release|Any CPU.ActiveCfg = Release|Any CPU {79C12C80-B27B-41FB-AE79-A3BB74CFA782}.Release|Any CPU.Build.0 = Release|Any CPU - {6D6F0582-4AA1-42B3-887F-415375263F0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6D6F0582-4AA1-42B3-887F-415375263F0E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6D6F0582-4AA1-42B3-887F-415375263F0E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6D6F0582-4AA1-42B3-887F-415375263F0E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -673,7 +667,6 @@ Global {44982E0D-C8C6-42DC-9F8F-714981F27CE6} = {7CB2F02E-03FA-4FFF-89A5-C51F107623FD} {79C12C80-B27B-41FB-AE79-A3BB74CFA782} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB} {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {A49299FB-C5CD-4E0E-B7E1-B7867BBD67CC} - {6D6F0582-4AA1-42B3-887F-415375263F0E} = {3862190B-E2C5-418E-AFDC-DB281FB5C705} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521} diff --git a/docs/logs/getting-started-console-di/ExampleService.cs b/docs/logs/getting-started-console-di/ExampleService.cs deleted file mode 100644 index 5a7c497729b..00000000000 --- a/docs/logs/getting-started-console-di/ExampleService.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; - -internal class ExampleService(ILogger logger) -{ - public void DoSomeWork() - { - logger.FoodPriceChanged("artichoke", 9.99); - - logger.FoodRecallNotice( - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - } -} diff --git a/docs/logs/getting-started-console-di/Program.cs b/docs/logs/getting-started-console-di/Program.cs deleted file mode 100644 index b1a149fe981..00000000000 --- a/docs/logs/getting-started-console-di/Program.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using OpenTelemetry.Logs; - -// Add services to the container, including OpenTelemetry with logging -var services = new ServiceCollection(); -services.AddOpenTelemetry().WithLogging(logging => logging.AddConsoleExporter()); -services.AddSingleton(); -ServiceProvider serviceProvider = services.BuildServiceProvider(); - -// Get the ExampleService object from the container -ExampleService service = serviceProvider.GetRequiredService(); - -service.DoSomeWork(); - -// Dispose before the application ends. -serviceProvider.Dispose(); - -internal static partial class LoggerExtensions -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} diff --git a/docs/logs/getting-started-console-di/README.md b/docs/logs/getting-started-console-di/README.md deleted file mode 100644 index bd3c661863c..00000000000 --- a/docs/logs/getting-started-console-di/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Console Application with Dependency Injection - -This example shows how create a console application with Dependency Injection via `ServiceCollection`. diff --git a/docs/logs/getting-started-console-di/getting-started-console-di.csproj b/docs/logs/getting-started-console-di/getting-started-console-di.csproj deleted file mode 100644 index 9e7b3138c7e..00000000000 --- a/docs/logs/getting-started-console-di/getting-started-console-di.csproj +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - From 13737c25a981a5788ee56581d7034e9149c2624c Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 11:26:44 -0800 Subject: [PATCH 09/10] Re-add redaction processor --- docs/logs/redaction/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index 0b3d2fb6cc4..af336a00c82 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -6,7 +6,11 @@ using OpenTelemetry.Logs; var sdk = OpenTelemetrySdk.Create(builder => builder - .WithLogging(logging => logging.AddConsoleExporter())); + .WithLogging(logging => + { + logging.AddProcessor(new MyRedactionProcessor()); + logging.AddConsoleExporter(); + })); var logger = sdk.GetLoggerFactory().CreateLogger(); From 619cd996898356b8923c63fca5cfe9019dac7834 Mon Sep 17 00:00:00 2001 From: Nathan Baird Date: Fri, 6 Dec 2024 12:50:59 -0800 Subject: [PATCH 10/10] line endings --- docs/logs/complex-objects/Program.cs | 12 +- docs/logs/customizing-the-sdk/Program.cs | 110 +++++++++---------- docs/logs/extending-the-sdk/Program.cs | 36 +++--- docs/logs/getting-started-console/Program.cs | 76 ++++++------- 4 files changed, 117 insertions(+), 117 deletions(-) diff --git a/docs/logs/complex-objects/Program.cs b/docs/logs/complex-objects/Program.cs index e44010edd01..8e9624d3095 100644 --- a/docs/logs/complex-objects/Program.cs +++ b/docs/logs/complex-objects/Program.cs @@ -5,9 +5,9 @@ using OpenTelemetry; using OpenTelemetry.Logs; -var sdk = OpenTelemetrySdk.Create(builder => builder - .WithLogging(logging => logging.AddConsoleExporter())); - +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); + var logger = sdk.GetLoggerFactory().CreateLogger(); var foodRecallNotice = new FoodRecallNotice @@ -19,9 +19,9 @@ CompanyName = "Contoso Fresh Vegetables, Inc.", }; -logger.FoodRecallNotice(foodRecallNotice); - -// Dispose SDK before the application ends. +logger.FoodRecallNotice(foodRecallNotice); + +// Dispose SDK before the application ends. sdk.Dispose(); internal static partial class LoggerExtensions diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index 40a041a2a2d..b576dcd91f2 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -1,55 +1,55 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; -using OpenTelemetry; -using OpenTelemetry.Logs; -using OpenTelemetry.Resources; - -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); - -using (logger.BeginScope(new List> -{ - new KeyValuePair("store", "Seattle"), -})) -{ - logger.FoodPriceChanged("truffle", 999.99); -} - -logger.FoodRecallNotice( - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - -// Dispose SDK before the application ends. -sdk.Dispose(); - -internal static partial class LoggerExtensions -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Logs; +using OpenTelemetry.Resources; + +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); + +using (logger.BeginScope(new List> +{ + new KeyValuePair("store", "Seattle"), +})) +{ + logger.FoodPriceChanged("truffle", 999.99); +} + +logger.FoodRecallNotice( + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + +// Dispose SDK before the application ends. +sdk.Dispose(); + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + + [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); +} diff --git a/docs/logs/extending-the-sdk/Program.cs b/docs/logs/extending-the-sdk/Program.cs index cf72e2e2f49..1fd745ec2be 100644 --- a/docs/logs/extending-the-sdk/Program.cs +++ b/docs/logs/extending-the-sdk/Program.cs @@ -11,17 +11,17 @@ public class Program { public static void Main() { - 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 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 @@ -54,13 +54,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. + 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(); } diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 199a33c5aa4..ba366c74e86 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -1,38 +1,38 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; -using OpenTelemetry; -using OpenTelemetry.Logs; - -var sdk = OpenTelemetrySdk.Create(builder => builder - .WithLogging(logging => logging.AddConsoleExporter())); - -var logger = sdk.GetLoggerFactory().CreateLogger(); - -logger.FoodPriceChanged("artichoke", 9.99); - -logger.FoodRecallNotice( - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - -// Dispose SDK before the application ends. -sdk.Dispose(); - -internal static partial class LoggerExtensions -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Logs; + +var sdk = OpenTelemetrySdk.Create(builder => builder + .WithLogging(logging => logging.AddConsoleExporter())); + +var logger = sdk.GetLoggerFactory().CreateLogger(); + +logger.FoodPriceChanged("artichoke", 9.99); + +logger.FoodRecallNotice( + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + +// Dispose SDK before the application ends. +sdk.Dispose(); + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + + [LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); +}