All Products
Search
Document Center

Simple Log Service:Import trace data from C# applications to Simple Log Service by using OpenTelemetry SDK for .NET

Last Updated:Aug 25, 2023

This topic describes how to import trace data from C# applications to Simple Log Service by using OpenTelemetry SDK for .NET.

Prerequisites

  • A trace instance is created. For more information, see Create a trace instance.

  • A .NET Framework development environment is set up.

    Note

    The import method in this topic supports all official versions of .NET Framework except for .NET Framework 3.5 SP1. For more information, see .NET Core and .NET Framework.

Procedure

  1. Add dependencies.

    dotnet add package OpenTelemetry --version 1.2.0-beta1
    dotnet add package OpenTelemetry.Exporter.Console --version 1.2.0-beta1
    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.2.0-beta1
    dotnet add package OpenTelemetry.Extensions.Hosting --version 1.0.0-rc8
    dotnet add package OpenTelemetry.Instrumentation.AspNetCore --version 1.0.0-rc8
    dotnet add package Grpc.Core --version 2.36.4
  2. Run code.

    Replace the variables in the following code with the actual values. For more information about the variables, see Variables.

    using System;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using System.Diagnostics;
    using System.Collections.Generic;
    using OpenTelemetry.Resources;
    using Grpc.Core;
    
    namespace mydemo
    {
        class Program
        {
             private static readonly ActivitySource MyActivitySource = new ActivitySource(
            "MyCompany.MyProduct.MyLibrary");
    
            static void Main(string[] args) {
                using var tracerProvider = Sdk.CreateTracerProviderBuilder()
               .SetSampler(new AlwaysOnSampler())
               .AddSource("MyCompany.MyProduct.MyLibrary")
               .AddOtlpExporter(opt => {
                    opt.Endpoint = new Uri("${endpoint}");
                    opt.Headers = "x-sls-otel-project=${project},x-sls-otel-instance-id=${instance},x-sls-otel-ak-id=${access-key-id},x-sls-otel-ak-secret=${access-key-secret}";
               })
                .SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder.CreateDefault()
               .AddAttributes(new Dictionary<string, object> {
                   {"service.name", "${service}" },
                   {"service.version","${version}"},
                   {"service.host","${host}"},
                   {"service.namespace","${service.namespace}"}
                   }))
               .Build();
    
                using (var activity = MyActivitySource.StartActivity("SayHello"))
                {
                    activity?.SetTag("foo", 1);
                    activity?.SetTag("bar", "Hello, World!");
                    activity?.SetTag("baz", new int[] { 1, 2, 3 });
                }
                Console.WriteLine("Hello World!");
    
            }
        }
    }
    Table 1. Variables

    Variable

    Description

    Example

    ${service}

    The name of the service. Specify the value based on your business requirements.

    payment

    ${version}

    The version of the service. We recommend that you specify the version in the va.b.c format.

    v0.1.2

    ${host}

    The hostname.

    localhost

    ${endpoint}

    The endpoint of the Simple Log Service project. Format: https://${project}.${region-endpoint}:Port.

    • ${project}: the name of the Simple Log Service project.

    • ${region-endpoint}: the Simple Log Service endpoint for the region where the project resides. You can access Simple Log Service by using an internal or public endpoint. An internal endpoint can be accessed over the classic network or a virtual private cloud (VPC). A public endpoint can be accessed over the Internet. For more information, see Endpoints.

    • Port: the port number. The value is fixed as 10010.

    https://test-project.cn-hangzhou.log.aliyuncs.com:10010

    ${project}

    The name of the Simple Log Service project.

    test-project

    ${instance}

    The ID of the trace instance. For more information, see Create a trace instance.

    instance-traces

    ${access-key-id}

    The AccessKey ID of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a RAM user that has only the write permissions on the Simple Log Service project. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to grant the write permissions on a specified project to a RAM user, see Use custom policies to grant permissions to a RAM user. For more information about how to obtain an AccessKey pair, see AccessKey pair.

    LTAI4Fvyv****

    ${access-key-secret}

    The AccessKey secret of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a RAM user that has only the write permissions on the Simple Log Service project.

    HfJEw25sYldO****

    ${service.namespace}

    The namespace to which the service belongs.

    order

What to do next