.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
-
Install OpenTelemetry .NET Auto-Instrumentation
Download and install the auto-instrumentation agent:
# Download the installation scriptcurl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O# Make it executable and runchmod +x otel-dotnet-auto-install.sh./otel-dotnet-auto-install.sh# Download and run the installation scriptInvoke-WebRequest -Uri "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.ps1" -OutFile "otel-dotnet-auto-install.ps1".\otel-dotnet-auto-install.ps1 -
Verify Installation
Check that the installation was successful:
# Verify installation directory existsls -la $HOME/.otel-dotnet-auto# Check for key filesls -la $HOME/.otel-dotnet-auto/instrument.shls -la $HOME/.otel-dotnet-auto/net/# Verify installation directory existsGet-ChildItem "$env:USERPROFILE\.otel-dotnet-auto"# Check for key filesGet-ChildItem "$env:USERPROFILE\.otel-dotnet-auto\instrument.cmd"
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=trueexport OTEL_SERVICE_NAME="<your_service_name>"export OTEL_TRACES_EXPORTER=otlpexport OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"export OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"export OTEL_TRACES_SAMPLER="always_on"export OTEL_LOG_LEVEL=errorexport OTEL_RESOURCE_ATTRIBUTES="deployment.environment=local"$env:OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED="true"$env:OTEL_SERVICE_NAME="<your_service_name>"$env:OTEL_TRACES_EXPORTER="otlp"$env:OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"$env:OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"$env:OTEL_TRACES_SAMPLER="always_on"$env:OTEL_LOG_LEVEL="error"$env: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
cmd call "%USERPROFILE%\.otel-dotnet-auto\instrument.cmd"
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=trueexport OTEL_SERVICE_NAME="<your_service_name>"export OTEL_TRACES_EXPORTER=otlpexport OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"export OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"export OTEL_TRACES_SAMPLER="always_on"export OTEL_LOG_LEVEL=errorexport 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 Releasedotnet run --configuration ReleaseCreate start.cmd:
@echo off
REM Set OpenTelemetry environment variablesset OTEL_DOTNET_AUTO_INSTRUMENTATION_ENABLED=trueset OTEL_SERVICE_NAME="<your_service_name>"set OTEL_TRACES_EXPORTER=otlpset OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"set OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"set OTEL_TRACES_SAMPLER=always_on
echo Starting .NET application with OpenTelemetry auto-instrumentation...echo Service Name: %OTEL_SERVICE_NAME%echo OTLP Endpoint: %OTEL_EXPORTER_OTLP_ENDPOINT%
REM Source the auto-instrumentationcall "%USERPROFILE%\.otel-dotnet-auto\instrument.cmd"
REM Build and run the applicationdotnet build --configuration Releasedotnet run --configuration ReleaseVerify 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=trueexport OTEL_DOTNET_AUTO_INSTRUMENTATION_HTTPCLIENT_ENABLED=trueexport OTEL_DOTNET_AUTO_INSTRUMENTATION_SQLCLIENT_ENABLED=falseOnce you run your application with the proper environment setup, it will automatically start sending telemetry data to Last9.