This example shows how to configure the OpenTelemetry .NET SDK’s logger provider to get logs to Honeycomb or an OpenTelemetry Collector.
First, install the OpenTelemetry SDK and OTLP exporter packages:
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
Next, create and configure the logger providers during application setup:
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;
// Configure logger factory with OpenTelemetry and log exporter
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddOtlpExporter();
});
});
// use the logger factory to create an ILogger logger instance
var logger = loggerFactory.CreateLogger<Program>();
// use the logger in your application
logger.LogInformation("Something interesting happened");
Finally, to send logs to Honeycomb, run your application with some environment variables to set the service name and to configure the exporter:
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
Alternatively, to send logs to an OpenTelemetry collector, use these environment variables:
OTEL_SERVICE_NAME="my-service" \
OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
OTEL_EXPORTER_OTLP_INSECURE=true \
dotnet run