Skip to content

Latest commit

 

History

History
119 lines (96 loc) · 4.58 KB

File metadata and controls

119 lines (96 loc) · 4.58 KB

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 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:                 344095174
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:                 1338249384
LogRecord.EventName:               FoodRecallNotice

...

Congratulations! You are now collecting logs using OpenTelemetry.

What does the above program do?

The program has created a logging pipeline by instantiating a LoggerFactory instance, with OpenTelemetry added as a logging provider. OpenTelemetry SDK is then configured with a ConsoleExporter to export the logs to the console for demonstration purpose (note: ConsoleExporter is not intended for production usage, other exporters such as OTLP Exporter should be used instead).

The LoggerFactory instance is used to create an ILogger instance, which is used to do the actual logging.

Following the .NET logging best practice, compile-time logging source generation has been used across the example, which delivers high performance, structured logging, and type-checked parameters:

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);

    ...
}

Note

For applications which use ILogger with dependency injection (DI) (e.g. ASP.NET Core and .NET Worker), the common practice is to add OpenTelemetry as a logging provider to the DI logging pipeline, rather than set up a completely new logging pipeline by creating a new LoggerFactory instance.

Refer to the Getting Started with OpenTelemetry .NET Logs in 5 Minutes - ASP.NET Core Application tutorial to learn more.

Learn more