diff --git a/OpenTelemetry.sln b/OpenTelemetry.sln index c109bf1075..994b28cbf7 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", "{3539AA71-4AC0-4946-BCB3-063670785AED}" +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 + {3539AA71-4AC0-4946-BCB3-063670785AED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3539AA71-4AC0-4946-BCB3-063670785AED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3539AA71-4AC0-4946-BCB3-063670785AED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3539AA71-4AC0-4946-BCB3-063670785AED}.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} + {3539AA71-4AC0-4946-BCB3-063670785AED} = {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 new file mode 100644 index 0000000000..5a7c497729 --- /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 0000000000..b1a149fe98 --- /dev/null +++ b/docs/logs/getting-started-console-di/Program.cs @@ -0,0 +1,35 @@ +// 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 new file mode 100644 index 0000000000..bd3c661863 --- /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 0000000000..9e7b3138c7 --- /dev/null +++ b/docs/logs/getting-started-console-di/getting-started-console-di.csproj @@ -0,0 +1,6 @@ + + + + + +