CloudYxt.OpenTelemetry 1.1.0

dotnet add package CloudYxt.OpenTelemetry --version 1.1.0
                    
NuGet\Install-Package CloudYxt.OpenTelemetry -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CloudYxt.OpenTelemetry" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CloudYxt.OpenTelemetry" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CloudYxt.OpenTelemetry" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CloudYxt.OpenTelemetry --version 1.1.0
                    
#r "nuget: CloudYxt.OpenTelemetry, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CloudYxt.OpenTelemetry@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CloudYxt.OpenTelemetry&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CloudYxt.OpenTelemetry&version=1.1.0
                    
Install as a Cake Tool

CloudYxt .Net Core针对OpenTelemetry常规操作库

appsettings.json的最精简配置(配置目标和数据流)

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "OpenTelemetry": {    
    "Exporter": {
      "Endpoint": "http://xxxx/api/default",
      "Authorization": "Basic 认证BASE64",
      "StreamName": "数据流名称"
    }
  }
}

appsettings.json的完整配置方法

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "OpenTelemetry": {
    "Enabled": true,

    //服务名称和版本
    "Service": {      
      "Name": "CloudYxt.OpenTelemetry.Debug",   //未配置时自动取程序集名称
      "Version": "1.0.0",                       //未配置时自动取程序集版本号
      "Environment": "Production"               //可选
    },

    //扩展字段
    "ResourceAttributes": {
      "company": "xxx",
      "department": "部门",
      "system": ""
    },

    "Exporter": {
      "Endpoint": "http://xxxx/api/default",
      "Authorization": "Basic 认证BASE64",
      "StreamName": "数据流名称"
    },

    // 注意:未配置时默认为"Enabled": true
    "Logging": {
      "Enabled": true
    },

    // 注意:未配置时默认为"Enabled": false
    "Metrics": {
      "Enabled": true,
      "Runtime": true,
      "AspNetCore": true,
      "HttpClient": true
    },

    // 注意:未配置时默认为"Enabled": false
    "Tracing": {
      "Enabled": true,
      "AspNetCore": true,
      "HttpClient": true,
      "Sampling": 1.0
    }
  }
}

如果是 Web API

var builder = WebApplication.CreateBuilder(args);

// 添加日志
builder.Logging.AddOpenTelemetryLoggingExtensions(builder.Configuration);

// 使用无扩展字段
builder.Services.AddOpenTelemetryExtensions(builder.Configuration);
// 或
// 使用自定义扩展字段
builder.Services.AddOpenTelemetryExtensions(builder.Configuration, new Dictionary<string, string>
{
    ["application_role"] = "i'am a webapi"
});

builder.Services.AddControllers();

var app = builder.Build();

app.MapControllers();

app.Run();

如果是 Worker Service

在Program.cs中定义

var builder = Host.CreateApplicationBuilder(args);

// 添加日志
builder.Logging.AddOpenTelemetryLoggingExtensions(builder.Configuration);

// 使用无扩展字段
builder.Services.AddOpenTelemetryExtensions(builder.Configuration);
// 或
// 使用自定义扩展字段
builder.Services.AddOpenTelemetryExtensions(builder.Configuration, new Dictionary<string, string>
{
    ["application_role"] = "i'am a worker"
    ["host_ip"] = NetworkHelper.GetLocalIPv4()
});

var host = builder.Build();

await host.RunAsync();

日志方法扩展

// 将一个对象格式化为字符串
_logger.LogInformation("result is {json_result}", json.ToLogJson());

获取本机IPv4地址

// 获取本机首选IPv4地址
NetworkHelper.GetLocalIPv4()
// 获取本机全部IPv4地址
NetworkHelper.GetLocalIPv4All()
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 40 9/3/2026
1.0.0 39 9/2/2026

CloudYxt .Net Core针对OpenTelemetry常规操作库。

1.1.0
增加NetworkHelper.GetLocalIPv4()和NetworkHelper.GetLocalIPv4All()方法

1.0.0
实现OpenTelemetry的配置简化