Public documentation

Documentation indexRead as Markdown

C# and .NET

Languages · Shared configuration · Verification

This console example targets .NET 8 or later and OpenTelemetry .NET 1.19.1. Upstream traces, metrics, and logs are Stable as checked on 2026-09-28. The sample emits only a trace using the .NET ActivitySource API.

Install

dotnet new console -n CorridoraExample
cd CorridoraExample
dotnet add package OpenTelemetry --version 1.19.1
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.19.1

Start the local Collector. Replace Program.cs with:

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

const string sourceName = "corridora.examples.dotnet";
using var source = new ActivitySource(sourceName);
using var provider = Sdk.CreateTracerProviderBuilder()
    .SetResourceBuilder(ResourceBuilder.CreateDefault()
        .AddService("corridora-dotnet", serviceVersion: "example-1")
        .AddAttributes(new Dictionary<string, object> {
            ["deployment.environment.name"] = "development"
        }))
    .AddSource(sourceName)
    .AddOtlpExporter(options => {
        options.Protocol = OtlpExportProtocol.HttpProtobuf;
        options.Endpoint = new Uri(
            Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
            ?? "http://127.0.0.1:4318/v1/traces");
        var key = Environment.GetEnvironmentVariable("CORRIDORA_INGEST_KEY");
        if (!string.IsNullOrEmpty(key)) {
            options.Headers = "X-CORRIDORA-KEY=" + key;
        }
    })
    .Build();

using (var activity = source.StartActivity("example.calculate")) {
    if (activity is null) {
        throw new InvalidOperationException("ActivitySource has no recording listener.");
    }
    activity.SetTag("example.language", "csharp");
    await Task.Delay(50);
    Console.WriteLine($"trace_id={activity.TraceId}");
}
if (!provider.ForceFlush(10000)) {
    Console.Error.WriteLine("Trace flush did not finish successfully.");
}

Run the sample:

dotnet run

The exporter is explicitly HTTP/protobuf. The program reads the signal-specific endpoint itself and defaults to the local Collector. Because the code sets options.Endpoint, changing only the generic OTEL_EXPORTER_OTLP_ENDPOINT does not redirect this example.

Integrate with an application

Register every custom ActivitySource name with .AddSource(...). A missing subscription or a sampling decision can make StartActivity return null; instrumentation code in an application should normally tolerate that. The sample throws to make a setup mistake visible.

For ASP.NET Core, add OpenTelemetry.Extensions.Hosting and the compatible OpenTelemetry.Instrumentation.AspNetCore and OpenTelemetry.Instrumentation.Http packages. Register builder.Services.AddOpenTelemetry().WithTracing(...), add ASP.NET Core/HTTP instrumentation, and apply the OTLP exporter options shown above. The host then owns provider lifetime; do not construct and dispose a new provider per request.

The separate .NET automatic instrumentation distribution uses runtime startup/profiler configuration. Installing these NuGet SDK packages does not install that agent. Choose either agent-owned or application-owned setup and check that the same instrumentation is not installed twice.

Context, logs, and metrics

Activity.Current carries the active trace context through ordinary asynchronous .NET execution. Supported server/client instrumentation handles W3C headers. If you suppress execution context flow, create threads manually, or use a custom queue, propagate the parent explicitly.

Correlated application logs require an OpenTelemetry ILogger provider and OTLP log exporter; logs emitted while an Activity is current can carry its trace and span IDs. Console.WriteLine in the sample is only local verification output.

Metrics use System.Diagnostics.Metrics.Meter and a meter provider that subscribes with .AddMeter(...), plus an OTLP metric exporter. Use the matching signal URL /v1/metrics; logs use /v1/logs. Reuse the service resource for all three providers and validate each signal separately.

Lifecycle, direct export, and verification

End/dispose activities before flushing and disposing the provider. Keep the provider alive for the entire application. A host shutdown or short-lived job needs enough time to finish its export; abrupt termination can lose queued telemetry.

For direct Corridora export, securely load the key as described in shared configuration, then set the full trace URL:

export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="${CORRIDORA_OTLP_ENDPOINT%/}/v1/traces"
dotnet run

This program reads CORRIDORA_INGEST_KEY and builds the ingest header explicitly. Keep it unset when only the local Collector should hold the credential.

In Corridora, search for service corridora-dotnet and the printed ID. Expect example.calculate of approximately 50 milliseconds. A null activity points to registration/sampling; a missing exported span points to transport, lifecycle, or visibility. Continue with verification.