Before you can view the trace data of your application in the Tracing Analysis console, you must use a client to submit the trace data to Tracing Analysis. This topic describes how to use an OpenTelemetry client to submit the data of .NET applications. The method also applies to C# applications.

Prerequisites

Sample code

Download the sample code from dotnet-demo.

Use OpenTelemetry SDK for .NET to manually instrument an application

In this example, OpenTelemetry SDK for .NET 6.0 is used.

  1. Go to the dotnet-demo/opentelemetry-demo directory and add the following OpenTelemetry dependencies that are required to manually instrument the application:
    dotnet add package OpenTelemetry
    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
    dotnet add package OpenTelemetry.Extensions.Hosting
  2. In the OpentelemetryExporterDemo.cs file, create an OpenTelemetry TracerProvider, add an HTTP-based OtlpExporter, and then replace the endpoint that are used to submit the application name and data.
    using System.Diagnostics;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using OpenTelemetry.Resources;
    using OpenTelemetry.Exporter;
    
    namespace Demo
    {
        internal static class OpentelemetryExporterDemo
        {
            internal static void Run()
            {
                Console.WriteLine("otlp running");
                // Use OpenTelemetry to submit the name of the application.
                var serviceName = "otlp-test";
                using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                    .AddSource(serviceName)
                    .SetResourceBuilder(
                    ResourceBuilder.CreateDefault().AddService(serviceName))
                    .AddOtlpExporter(opt =>
                                     {
                                         // Replace the value of the parameter with the endpoint that you obtained in the "Prerequisites" section of this topic.
                                         opt.Endpoint = new Uri("http://localhost/api/otlp/traces");
                                         // Submit data over HTTP.
                                         opt.Protocol = OtlpExportProtocol.HttpProtobuf;
                                     })
                    .Build();
                for(int i = 0; i<10; i++)
                {
                    var MyActivitySource = new ActivitySource(serviceName);
                    using var activity = MyActivitySource.StartActivity("SayHello");
                    activity?.SetTag("bar", "Hello World");
                }
            }
        }
    }
  3. Run the following command in the current path:
    dotnet run --framework:net6.0
  4. Start the on-premises program.In the Tracing Analysis console, search for the required application by name and then view the data that is submitted by the application.