4.1 KiB
Getting Started with OpenTelemetry .NET Logs in 5 Minutes - Console Application
First, download and install the .NET SDK on your computer.
Create a new console application and run it:
dotnet new console --output getting-started
cd getting-started
dotnet run
You should see the following output:
Hello World!
Install the latest Microsoft.Extensions.Logging package:
dotnet add package Microsoft.Extensions.Logging
Install the OpenTelemetry.Exporter.Console package:
dotnet add package OpenTelemetry.Exporter.Console
Update the Program.cs file with the code from Program.cs.
Run the application again (using dotnet run) and you should see the log output
on the console.
LogRecord.Timestamp: 2023-09-15T06:07:03.5502083Z
LogRecord.CategoryName: Program
LogRecord.Severity: Info
LogRecord.SeverityText: Information
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
...
LogRecord.Timestamp: 2023-09-15T06:07:03.5683511Z
LogRecord.CategoryName: Program
LogRecord.Severity: Fatal
LogRecord.SeverityText: Critical
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
...
Congratulations! You are now collecting logs using OpenTelemetry.
What does the above program do?
The program has a
LoggerFactory
with OpenTelemetry added as a
LoggerProvider.
This LoggerFactory is used to create an
ILogger
instance, which is then used to do the logging. Compile-time logging source
generation
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 for different application types
Certain types of applications (e.g. ASP.NET
Core and .NET
Worker) 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 for an example which shows
how to add OpenTelemetry to the logging pipeline already setup by the
application.