Skip to content
Last9 named a Gartner Cool Vendor in AI for SRE Observability for 2025! Read more →
Last9

.NET Core

Instrument your .NET application with OpenTelemetry to send traces, metrics, and logs to Last9

Use OpenTelemetry to automatically instrument your .NET application and send telemetry data to Last9 without modifying your application code.

Prerequisites

  • .NET SDK 8.0 or later

Installation

  1. Install OpenTelemetry .NET Auto-Instrumentation

    Download and install the auto-instrumentation agent:

    # Download the installation script
    curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
    # Make it executable and run
    chmod +x otel-dotnet-auto-install.sh
    ./otel-dotnet-auto-install.sh
  2. Verify Installation

    Check that the installation was successful:

    # Verify installation directory exists
    ls -la $HOME/.otel-dotnet-auto
    # Check for key files
    ls -la $HOME/.otel-dotnet-auto/instrument.sh
    ls -la $HOME/.otel-dotnet-auto/net/

Setup Auto-Instrumentation

Environment Variables

Set the required environment variables for Last9. Replace the placeholder values with your actual Last9 configuration:

export OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED=true
export OTEL_SERVICE_NAME="<your_service_name>"
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"
export OTEL_TRACES_SAMPLER="always_on"
export OTEL_LOG_LEVEL=error
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=local"

Source the Instrumentation Script

Before running your application, source the instrumentation script:

bash . $HOME/.otel-dotnet-auto/instrument.sh

Application Code

Minimal Application Setup

With auto-instrumentation, your application code requires no OpenTelemetry-specific packages or configuration. Keep your Program.cs clean.

Project File (.csproj)

Your project file should be minimal - no OpenTelemetry packages needed:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MyDotNetApi</RootNamespace>
</PropertyGroup>
</Project>

Running Your Application

Complete Startup Script

Create a startup script that sets up everything:

Create start.sh:

#!/bin/bash
# Set OpenTelemetry environment variables
export OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED=true
export OTEL_SERVICE_NAME="<your_service_name>"
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"
export OTEL_TRACES_SAMPLER="always_on"
export OTEL_LOG_LEVEL=error
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=local"
echo "Starting .NET application with OpenTelemetry auto-instrumentation..."
echo "Service Name: $OTEL_SERVICE_NAME"
echo "OTLP Endpoint: $OTEL_EXPORTER_OTLP_ENDPOINT"
# Source the auto-instrumentation
. $HOME/.otel-dotnet-auto/instrument.sh
# Build and run the application
dotnet build --configuration Release
dotnet run --configuration Release

Verify Telemetry

You should see debug output in your console showing:

  • OpenTelemetry initialization
  • Span creation for HTTP requests
  • Trace export attempts to Last9

What Gets Automatically Instrumented

The auto-instrumentation automatically captures:

  • HTTP requests (ASP.NET Core)
  • Database calls (Entity Framework, SQL Client, MongoDB, etc.)
  • HTTP client calls (HttpClient)
  • Message queues (RabbitMQ, Azure Service Bus, etc.)
  • Custom logs via ILogger
  • gRPC calls
  • Redis operations

Advanced Configuration

Custom Resource Attributes

Add additional resource attributes to identify your service better:

export OTEL_RESOURCE_ATTRIBUTES="service.name=my-dotnet-api,deployment.environment=production,service.version=1.2.3"

Disable Specific Instrumentations

Disable instrumentations you don’t need:

export OTEL_DOTNET_AUTO_INSTRUMENTATION_ASPNETCORE_ENABLED=true
export OTEL_DOTNET_AUTO_INSTRUMENTATION_HTTPCLIENT_ENABLED=true
export OTEL_DOTNET_AUTO_INSTRUMENTATION_SQLCLIENT_ENABLED=false

Once you run your application with the proper environment setup, it will automatically start sending telemetry data to Last9.