Skip to content

Commit

Permalink
Improve logging getting-started example for console applications (#4740)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang committed Aug 4, 2023
1 parent 01c3856 commit a76564e
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 157 deletions.
11 changes: 2 additions & 9 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "metrics", "metrics", "{3277
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "logs", "logs", "{3862190B-E2C5-418E-AFDC-DB281FB5C705}"
ProjectSection(SolutionItems) = preProject
docs\logs\getting-started\README.md = docs\logs\getting-started\README.md
docs\logs\getting-started-console\README.md = docs\logs\getting-started-console\README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MicroserviceExample", "MicroserviceExample", "{4D492D62-5150-45F9-817F-C99562E364E2}"
Expand Down Expand Up @@ -186,7 +186,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Instrumentati
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.GrpcService", "examples\GrpcService\Examples.GrpcService.csproj", "{DB942F5A-D571-4DEA-B1A7-B6BE0E24E6ED}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started", "docs\logs\getting-started\getting-started.csproj", "{B3F03725-23A0-4582-9526-F6A7E38F35CC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-console", "docs\logs\getting-started-console\getting-started-console.csproj", "{B3F03725-23A0-4582-9526-F6A7E38F35CC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.InMemory", "src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj", "{9BCEA68B-50E2-4A3A-93E6-B51AF612BCC1}"
EndProject
Expand All @@ -210,8 +210,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "customizing-the-sdk", "docs
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Tests.Stress", "test\OpenTelemetry.Tests.Stress\OpenTelemetry.Tests.Stress.csproj", "{2770158A-D220-414B-ABC6-179371323579}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "source-generation", "docs\logs\source-generation\source-generation.csproj", "{1F6CC903-04C9-4E7C-B388-C215C467BFB9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-prometheus-grafana", "docs\metrics\getting-started-prometheus-grafana\getting-started-prometheus-grafana.csproj", "{41B784AA-3301-4126-AF9F-1D59BD04B0BF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.SemanticConventions", "src\OpenTelemetry.SemanticConventions\OpenTelemetry.SemanticConventions.csproj", "{D4519DF6-CC72-4AC4-A851-E21383098D11}"
Expand Down Expand Up @@ -487,10 +485,6 @@ Global
{2770158A-D220-414B-ABC6-179371323579}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2770158A-D220-414B-ABC6-179371323579}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2770158A-D220-414B-ABC6-179371323579}.Release|Any CPU.Build.0 = Release|Any CPU
{1F6CC903-04C9-4E7C-B388-C215C467BFB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F6CC903-04C9-4E7C-B388-C215C467BFB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F6CC903-04C9-4E7C-B388-C215C467BFB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F6CC903-04C9-4E7C-B388-C215C467BFB9}.Release|Any CPU.Build.0 = Release|Any CPU
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -618,7 +612,6 @@ Global
{0C3E7D40-E0B3-4B77-8139-0E85C3600688} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{1F9D7748-D099-4E25-97F5-9C969D6FF969} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{81234AFA-B4E7-4D0D-AB97-FD559C78EDA2} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{1F6CC903-04C9-4E7C-B388-C215C467BFB9} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
{41B784AA-3301-4126-AF9F-1D59BD04B0BF} = {3277B1C0-BDFE-4460-9B0D-D9A661FB48DB}
{6C7A1595-36D6-4229-BBB5-5A6B5791791D} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
{9A07D215-90AC-4BAF-BCDB-73D74FD3A5C5} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ repo.

If you are new here, please read the getting started docs:

* [logs](./docs/logs/getting-started/README.md)
* metrics: [ASP.NET Core](./docs/metrics/getting-started-aspnetcore/README.md) |
* Logs: [Console](./docs/logs/getting-started-console/README.md)
* Metrics: [ASP.NET Core](./docs/metrics/getting-started-aspnetcore/README.md) |
[Console](./docs/metrics/getting-started-console/README.md)
* traces: [ASP.NET Core](./docs/trace/getting-started-aspnetcore/README.md) |
* Traces: [ASP.NET Core](./docs/trace/getting-started-aspnetcore/README.md) |
[Console](./docs/trace/getting-started-console/README.md)

This repository includes multiple installable components, available on
Expand Down
6 changes: 3 additions & 3 deletions docs/logs/correlation/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Logs correlation

The getting started docs for [logs](../getting-started/README.md) and
The getting started docs for [logs](../getting-started-console/README.md) and
[traces](../../trace/getting-started-console/README.md) showed how to emit logs
and traces independently, and export them to console exporter.

Expand Down Expand Up @@ -54,8 +54,8 @@ Resource associated with Activity:

As you can see, the `LogRecord` automatically had the `TraceId`, `SpanId` fields
matching the ones from the `Activity`. In [the logs getting
started](../getting-started/README.md) doc, the logging was done outside of an
`Activity` context, hence these fields in `LogRecord` were not populated.
started](../getting-started-console/README.md) doc, the logging was done outside
of an `Activity` context, hence these fields in `LogRecord` were not populated.

## Learn more

Expand Down
File renamed without changes.
117 changes: 117 additions & 0 deletions docs/logs/getting-started-console/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Getting Started with OpenTelemetry .NET Logs in 5 Minutes - Console Application

First, download and install the [.NET
SDK](https://dotnet.microsoft.com/download) on your computer.

Create a new console application and run it:

```sh
dotnet new console --output getting-started
cd getting-started
dotnet run
```

You should see the following output:

```text
Hello World!
```

Install the latest `Microsoft.Extensions.Logging` package:

```sh
dotnet add package Microsoft.Extensions.Logging
```

Install the
[OpenTelemetry.Exporter.Console](../../../src/OpenTelemetry.Exporter.Console/README.md)
package:

```sh
dotnet add package OpenTelemetry.Exporter.Console
```

Copy the [FoodSupplyLogs.cs](./FoodSupplyLogs.cs) and [Program.cs](./Program.cs)
files to the project folder.

Run the application again (using `dotnet run`) and you should see the log output
on the console.

```text
LogRecord.Timestamp: 2023-08-03T22:53:51.0194130Z
LogRecord.CategoryName: SourceGeneration.Program
LogRecord.Severity: Info
LogRecord.SeverityText: Information
LogRecord.FormattedMessage: Food `artichoke` price changed to `9.99`.
LogRecord.Body: Food `{name}` price changed to `{price}`.
LogRecord.Attributes (Key:Value):
name: artichoke
price: 9.99
OriginalFormat (a.k.a Body): Food `{name}` price changed to `{price}`.
LogRecord.EventId: 1
LogRecord.EventName: FoodPriceChanged
Resource associated with LogRecord:
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.6.0-alpha.1.55
service.name: unknown_service:getting-started
LogRecord.Timestamp: 2023-08-03T22:53:51.0403466Z
LogRecord.CategoryName: SourceGeneration.Program
LogRecord.Severity: Fatal
LogRecord.SeverityText: Critical
LogRecord.FormattedMessage: A `Food & Beverages` recall notice was published for `Contoso Salads` produced by `Contoso Fresh Vegetables, Inc.` (due to a possible health risk from Listeria monocytogenes).
LogRecord.Body: A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).
LogRecord.Attributes (Key:Value):
brandName: Contoso
productDescription: Salads
productType: Food & Beverages
recallReasonDescription: due to a possible health risk from Listeria monocytogenes
companyName: Contoso Fresh Vegetables, Inc.
OriginalFormat (a.k.a Body): A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).
LogRecord.EventId: 2
LogRecord.EventName: FoodRecallNotice
Resource associated with LogRecord:
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.6.0-alpha.1.55
service.name: unknown_service:getting-started
```

Congratulations! You are now collecting logs using OpenTelemetry.

What does the above program do?

The program has a
[`LoggerFactory`](https://docs.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerfactory)
with OpenTelemetry added as a
[LoggerProvider](https://docs.microsoft.com/dotnet/core/extensions/logging-providers).
This `LoggerFactory` is used to create an
[`ILogger`](https://docs.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger)
instance, which is then used to do the logging. [Compile-time logging source
generation](https://docs.microsoft.com/dotnet/core/extensions/logger-message-generator)
is used to achieve structured logging and better performance. The logs are sent to
the `OpenTelemetryLoggerProvider`, which is configured to export logs to
`ConsoleExporter`. `ConsoleExporter` simply displays it on the console.

> **Note**
> Certain types of applications (e.g. [ASP.NET
Core](https://learn.microsoft.com/aspnet/core) and [.NET
Worker](https://learn.microsoft.com/dotnet/core/extensions/workers)) have an
`ILogger` based logging pipeline set up by default. In such apps, enabling
OpenTelemetry should be done by adding OpenTelemetry as a provider to the
*existing* logging pipeline, and users should not create a new `LoggerFactory`
(which sets up a totally new logging pipeline). Also, obtaining `ILogger`
instance could be done differently as well. See [Example ASP.NET Core
application](../../../examples/AspNetCore/Program.cs) for an example which shows
how to add OpenTelemetry to the logging pipeline already setup by the
application.

## Learn more

* [Compile-time logging source
generation](https://docs.microsoft.com/dotnet/core/extensions/logger-message-generator)
* [Customizing the OpenTelemetry .NET SDK](../customizing-the-sdk/README.md)
* [Extending the OpenTelemetry .NET SDK](../extending-the-sdk/README.md)
45 changes: 0 additions & 45 deletions docs/logs/getting-started/Program.cs

This file was deleted.

81 changes: 0 additions & 81 deletions docs/logs/getting-started/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions docs/logs/source-generation/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions docs/logs/source-generation/source-generation.csproj

This file was deleted.

2 changes: 1 addition & 1 deletion src/OpenTelemetry.Exporter.Console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dotnet add package OpenTelemetry.Exporter.Console
See the individual "getting started" examples depending on the signal being
used:

* [Logs](../../docs/logs/getting-started/Program.cs)
* Logs: [Console](../../docs/logs/getting-started-console/README.md)
* Metrics: [ASP.NET Core](../../docs/metrics/getting-started-aspnetcore/README.md)
| [Console](../../docs/metrics/getting-started-console/README.md)
* Traces: [ASP.NET Core](../../docs/trace/getting-started-aspnetcore/README.md)
Expand Down
3 changes: 2 additions & 1 deletion src/OpenTelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ and enable the SDK, when they install a particular exporter.
## Getting started with Logging

If you are new to logging, it is recommended to first follow the [getting
started in 5 minutes](../../docs/logs/getting-started/README.md) guide to get up
started in 5 minutes - Console
Application](../../docs/logs/getting-started-console/README.md) guide to get up
and running.

While [OpenTelemetry
Expand Down

0 comments on commit a76564e

Please sign in to comment.