DEV Community

Carlos Mendible
Carlos Mendible

Posted on • Originally published at carlos.mendible.com on

ASP.NET Core OpenTelemetry Logging

As you may know I’ve been collaborating with Dapr and I’ve learned that one of the things it enables you to do is to collect traces with the use of the OpenTelemetry Collector and push the events to Azure Application Insights.

After some reading I went and check if I could also write my ASP.NET Core applications to log using the OpenTelemetry Log and Event record definition:

Field Name Description
Timestamp Time when the event occurred.
TraceId Request trace id.
SpanId Request span id.
TraceFlags W3C trace flag.
SeverityText The severity text (also known as log level).
SeverityNumber Numerical value of the severity.
Name Short event identifier.
Body The body of the log record.
Resource Describes the source of the log.
Attributes Additional information about the event.

So here is how you can do it:

Create an ASP.NET Core application

dotnet new webapi -n aspnet.opentelemetry
cd aspnet.opentelemetry
Enter fullscreen mode Exit fullscreen mode

Add a reference to the OpenTelemtry libraries

dotnet add package OpenTelemetry.Extensions.Hosting --prerelease
dotnet add package OpenTelemetry.Exporter.Console --prerelease
Enter fullscreen mode Exit fullscreen mode

Modify the CreateHostBuilder method in Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
      .ConfigureLogging(logging =>
      {
          logging.ClearProviders();
          logging.AddOpenTelemetry(options =>
          {
              options.AddProcessor(new SimpleExportProcessor<LogRecord>(new ConsoleExporter<LogRecord>(new ConsoleExporterOptions())));
          });
      })
      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder.UseStartup<Startup>();
      });
Enter fullscreen mode Exit fullscreen mode

The code first clears all logging providers and then adds OpenTelemetry using the SimpleExportProcessor and ConsoleExporter.

Please check the OpenTelemetry .NET API repo to learn more.

Modify the log settings

Edit the appsettings.Development.json in order to configure the default log settings using the OpenTelemetry provider:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
Enter fullscreen mode Exit fullscreen mode

Test the program

Run the following command to test the application.

dotnet run
Enter fullscreen mode Exit fullscreen mode

You should see the logs, formatted as OpenTelemtry log records, written in the console:

LogRecord.TraceId: 00000000000000000000000000000000
LogRecord.SpanId: 0000000000000000
LogRecord.Timestamp: 2021-01-08T10:36:26.1338054Z
LogRecord.EventId: 0
LogRecord.CategoryName: Microsoft.Hosting.Lifetime
LogRecord.LogLevel: Information
LogRecord.TraceFlags: None
LogRecord.State: Application is shutting down…

Both TraceId and SpanId will get filled when a request is handled by your application and if the call is made by Dapr it will respect the values sent by it so you can correlate traces and logs improving the observability of your solution.

Hope it helps! and please find the complete code here

Top comments (1)

Collapse
 
adambgr profile image
adambgr

Useful, thanks !