Updated on 2026-01-30 GMT+08:00

Reporting .NET Application Data Using OpenTelemetry

Huawei Cloud APM is compatible with OpenTelemetry and can directly receive the trace data reported using the OpenTelemetry SDK or Agent. This section describes how to use OpenTelemetry to connect a .NET application to APM and report trace data.

Constraint

.NET SDK 8 or later must be used.

Environment Preparation

  1. Prepare .NET SDK 8.
  2. Run the installation command:
    sudo apt-get update &&  sudo apt-get install -y dotnet-sdk-8.0
  3. Verify the environment:
    dotnet --version

Demo

  1. Initialize the application:
    dotnet new web
  2. Replace the content in the Program.cs file in the current directory with the following content:
    using System.Globalization;
    
    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    var logger = app.Logger;
    
    int RollDice()
    {
        return Random.Shared.Next(1, 7);
    }
    
    string HandleRollDice(string? player)
    {
        var result = RollDice();
        if (string.IsNullOrEmpty(player))
        {
            logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
        }
        else
        {
            logger.LogInformation("{player} is rolling the dice: {result}", player, result);
        }
        return result.ToString(CultureInfo.InvariantCulture);
    }
    
    app.MapGet("/rolldice/{player?}", HandleRollDice);
    app.Run();
  3. Replace the content in the launchSettings.json file in the Properties directory with the following content:
    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "profiles": {
        "http": {
          "commandName": "Project",
          "dotnetRunMessages": true,
          "launchBrowser": true,
          "applicationUrl": "http://localhost:8080",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
  4. Build and run the demo application in the directory where Program.cs is stored:
    dotnet build 
    dotnet run 
    curl  http://localhost:8080/rolldice

Non-Intrusive Access

  1. Install the dependencies for auto tracking:

    curl -L -O https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh 
    chmod +x otel-dotnet-auto-install.sh 
    ./otel-dotnet-auto-install.sh

  2. Add environment variables:

    export OTEL_TRACES_EXPORTER=otlp \
      OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
      OTEL_METRICS_EXPORTER=none \
      OTEL_LOGS_EXPORTER=none \
      OTEL_SERVICE_NAME=Application.Component.Environment \
      OTEL_EXPORTER_OTLP_ENDPOINT=Access address \
      OTEL_EXPORTER_OTLP_HEADERS="Authentication=Authentication information"
    
    . $HOME/.otel-dotnet-auto/instrument.sh

  3. Run the demo application:

    dotnet run &
    curl  http://localhost:8080/rolldice

  4. Log in to the APM console.
  5. Click on the left and choose Management & Governance > Application Performance Management.
  6. In the navigation pane, choose Link Trace > Metrics.
  7. In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.

  8. In the navigation pane, choose Link Trace > Tracing.