Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a console dependency injection logging project to docs section #6014

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
19 changes: 19 additions & 0 deletions docs/logs/getting-started-console-di/ExampleService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;

internal class ExampleService(ILogger<ExampleService> 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.");
}
}
35 changes: 35 additions & 0 deletions docs/logs/getting-started-console-di/Program.cs
Original file line number Diff line number Diff line change
@@ -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<ExampleService>();
ServiceProvider serviceProvider = services.BuildServiceProvider();

// Get the ExampleService object from the container
ExampleService service = serviceProvider.GetRequiredService<ExampleService>();

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);
}
3 changes: 3 additions & 0 deletions docs/logs/getting-started-console-di/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Console Application with Dependency Injection

This example shows how create a console application with Dependency Injection via `ServiceCollection`.

Check failure on line 3 in docs/logs/getting-started-console-di/README.md

View workflow job for this annotation

GitHub Actions / lint-md / run-markdownlint

Line length

docs/logs/getting-started-console-di/README.md:3:81 MD013/line-length Line length [Expected: 80; Actual: 102] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md013.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is something OTel should document. It shows getting started guides for simple console app and asp.net core (which implicitly cover DI). It is not recommended to keep expanding the amount of examples in this repo, unless there is a strong reason to.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
</ItemGroup>
</Project>
Loading