Example: Send OpenTelemetry Logs with the OpenTelemetry .NET SDK
Send OpenTelemetry logs from a .NET application to Honeycomb or an OpenTelemetry Collector using the OpenTelemetry .NET SDK logger provider.
This example shows how to configure the OpenTelemetry .NET SDK’s logger provider to send logs from a .NET application to Honeycomb or to an OpenTelemetry Collector.
1
Acquire Dependencies
Install the OpenTelemetry SDK and OTLP exporter packages:
During application setup, create and configure the logger provider:
Report incorrect code
Copy
using Microsoft.Extensions.Logging;using OpenTelemetry;using OpenTelemetry.Logs;// Configure a logger factory with OpenTelemetry and the OTLP log exporterusing var loggerFactory = LoggerFactory.Create(builder =>{ builder.AddOpenTelemetry(options => { options.AddOtlpExporter(); });});// Create an ILogger instance from the logger factoryvar logger = loggerFactory.CreateLogger<Program>();// Use the logger in your applicationlogger.LogInformation("Something interesting happened");
3
Run Your Application
Set environment variables to configure the exporter and define your service name, then run your application.To send logs directly to Honeycomb:
Report incorrect code
Copy
OTEL_SERVICE_NAME="my-service" \ OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io" \ OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=<your-api-key>" \ dotnet run
To send logs to an OpenTelemetry Collector instead:
Report incorrect code
Copy
OTEL_SERVICE_NAME="my-service" \ OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \ OTEL_EXPORTER_OTLP_INSECURE=true \ dotnet run