You need to enable JavaScript to run this app.
文档中心
日志服务

日志服务

复制全文
下载 pdf
.NET SDK
快速开始 .NET SDK
复制全文
下载 pdf
快速开始 .NET SDK
本文介绍如何快速使用日志服务 .NET SDK 实现基础的日志采集流程,包括创建日志项目、创建日志主题、写入日志和查询日志等操作。​
前提条件​
  • 已安装日志服务 .NET SDK。详细步骤请参考安装 .NET SDK。​
初始化客户端​
初始化 Client 实例之后,才可以向 TLS 服务发送请求。初始化时推荐通过环境变量动态获取火山引擎密钥等身份认证信息,以免 AccessKey 硬编码引发数据安全风险。​
初始化代码如下:​
​
VOLCENGINE_ACCESS_KEY_ID="your accessKeyId" VOLCENGINE_ACCESS_KEY_SECRET="your secretKey" VOLCENGINE_ENDPOINT="your host" VOLCENGINE_REGION="your region"​
​
示例代码​
如下示例代码介绍如何设置火山云权限配置、启动消费者、发送单条/多条消息、模拟任务处理等信息。​
初始化上报日志示例(PutLogs)​
​
using VolcengineTls.Request;​
using VolcengineTls.Response;​
​
namespace VolcengineTls.Examples​
{​
public class PutLogsExample​
{​
public async Task RunAsync()​
{​
// 设置火山云权限配置​
var vcConfig = new VolcengineConfig(​
region: Environment.GetEnvironmentVariable("VOLCENGINE_REGION"),​
endpoint: Environment.GetEnvironmentVariable("VOLCENGINE_ENDPOINT"),​
accessKeyId: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_ID"),​
accessKeySecret: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_SECRET"),​
securityToken: Environment.GetEnvironmentVariable("VOLCENGINE_SECURITY_TOKEN")​
);​
​
// 实例化tls sdk client​
var client = new Client(vcConfig);​
​
// 构建日志数据​
var logGroupList = new Pb.LogGroupList();​
var logGroup = new Pb.LogGroup();​
​
// 单个logGroup不超过10000条​
var num = 100;​
while (num > 0)​
{​
var log = new Pb.Log​
{​
Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),​
};​
​
var logContents = new List<Pb.LogContent>​
{​
new Pb.LogContent { Key = "k1", Value = $"value{num}" },​
new Pb.LogContent { Key = "k2", Value = $"value{num}" }​
};​
​
log.Contents.Add(logContents);​
​
logGroup.Logs.Add(log);​
​
num--;​
}​
​
logGroupList.LogGroups.Add(logGroup);​
​
// 构建请求​
var putLogsRequest = new PutLogsRequest(​
logGroupList: logGroupList,​
topicId: Environment.GetEnvironmentVariable("TOPIC_ID")​
);​
​
PutLogsResponse putLogsResponse = await client.PutLogs(putLogsRequest);​
}​
}​
}​
​
初始化上报日志示例(PutLogsV2)​
​
using System;​
using System.Collections.Generic;​
using System.Threading.Tasks;​
using VolcengineTls.Request;​
using VolcengineTls.Response;​
​
namespace VolcengineTls.Examples​
{​
public class PutLogsV2Example​
{​
public async Task RunAsync()​
{​
// 设置火山云权限配置​
var vcConfig = new VolcengineConfig(​
region: Environment.GetEnvironmentVariable("VOLCENGINE_REGION"),​
endpoint: Environment.GetEnvironmentVariable("VOLCENGINE_ENDPOINT"),​
accessKeyId: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_ID"),​
accessKeySecret: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_SECRET"),​
securityToken: Environment.GetEnvironmentVariable("VOLCENGINE_SECURITY_TOKEN")​
);​
​
// 实例化tls sdk client​
var client = new Client(vcConfig);​
​
/// 构建日志数据(封装的Pb数据)​
var logs = new List<Log>();​
// 单个logGroup不超过10000条​
var num = 100;​
while (num > 0)​
{​
var log = new Log(​
time: DateTimeOffset.UtcNow.ToUnixTimeSeconds(),​
contents: new List<LogContent>{​
new LogContent("k1", $"value{num}"),​
new LogContent("k2", $"value{num}"),​
}​
);​
​
logs.Add(log);​
​
num--;​
}​
​
// 构建请求​
var putLogsV2Request = new PutLogsV2Request(​
logs: logs,​
topicId: Environment.GetEnvironmentVariable("TOPIC_ID"),​
compressType: Consts.CompressLz4,​
hashKey: "",​
source: "your log source",​
fileName: "your log filename"​
);​
​
PutLogsResponse putLogsV2Response = await client.PutLogsV2(putLogsV2Request);​
}​
}​
}​
​
初始化 Producer 上报日志示例​
​
using System;​
using System.Threading;​
using VolcengineTls.Producer;​
​
namespace VolcengineTls.Examples​
{​
public class DefaultCallBackImp : ICallBack​
{​
public void Fail(ProducerResult result)​
{​
Console.WriteLine("put log to tls failed");​
}​
​
public void Success(ProducerResult result)​
{​
Console.WriteLine("put log to tls success");​
}​
}​
​
public class ProducerExample​
{​
public void Run()​
{​
// 设置火山云权限配置​
var vc = new VolcengineConfig(​
region: Environment.GetEnvironmentVariable("VOLCENGINE_REGION"),​
endpoint: Environment.GetEnvironmentVariable("VOLCENGINE_ENDPOINT"),​
accessKeyId: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_ID"),​
accessKeySecret: Environment.GetEnvironmentVariable("VOLCENGINE_ACCESS_KEY_SECRET"),​
securityToken: Environment.GetEnvironmentVariable("VOLCENGINE_SECURITY_TOKEN")​
);​
​
// 设置火山云配置,当前获取默认配置​
var config = new ProducerConfig(vc);​
// 实例化​
var producer = new ProducerImp(config);​
// 启动生产者​
producer.Start();​
​
/// 发送单条​
// 构造日志​
var keyNum = 2;​
var log = new Pb.Log​
{​
Time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),​
};​
​
for (var i = 0; i < keyNum; i++)​
{​
var logContent = new Pb.LogContent​
{​
Key = $"key{i}",​
Value = $"c#-value-test-{i}",​
};​
​
log.Contents.Add(logContent);​
}​
​
// 发送单条 或者使用SendLogV2​
producer.SendLog(​
topicId: Environment.GetEnvironmentVariable("TOPIC_ID"),​
log: log,​
source: "your log source",​
filename: "your log filename",​
shardHash: null,​
callback: new DefaultCallBackImp() // 不需要的话可以传null​
);​
​
/// 发送多条​
var logNum = 1000;​
var logGroup = new Pb.LogGroup();​
​
for (var j = 0; j < logNum; j++)​
{​
log = new Pb.Log​
{​
Time = DateTimeOffset.Now.ToUnixTimeSeconds(),​
};​
​
for (var i = 0; i < keyNum; i++)​
{​
var logContent = new Pb.LogContent​
{​
Key = $"no-{j}-key{i}",​
Value = $"no-{j}-c#-value-test-{i}",​
};​
​
log.Contents.add(logContent);​
}​
​
logGroup.Logs.Add(log);​
}​
​
producer.SendLogs(​
topicId: Environment.GetEnvironmentVariable("TOPIC_ID"),​
logs: logGroup,​
source: "your log source",​
filename: "your log filename",​
shardHash: null,​
callback: new DefaultCallBackImp() // 不需要的话可以传null​
);​
​
// 模拟其他任务处理​
Thread.Sleep(TimeSpan.FromSeconds(20));​
​
// 生产者关闭​
producer.Close();​
}​
}​
}​
​
运行 example 示例​
​
using System;​
​
namespace VolcengineTls.Examples​
{​
class Program​
{​
static void Main(string[] args)​
{​
try​
{​
Console.WriteLine("Starting example...");​
​
var example = new ProducerExample();​
​
example.Run(); // 同步方法​
// example.RunAsync().Wait(); // 异步方法​
​
Console.WriteLine("Example completed successfully.");​
}​
catch (AggregateException ex)​
{​
foreach (var innerEx in ex.InnerExceptions)​
{​
Console.WriteLine($"Error: {innerEx.Message}");​
if (innerEx.InnerException != null)​
{​
Console.WriteLine($"Inner Error: {innerEx.InnerException.Message}");​
}​
}​
}​
catch (Exception ex)​
{​
Console.log($"Error: {ex.Message}");​
if (ex.InnerException != null)​
{​
Console.WriteLine($"Inner Error: {ex.InnerException.Message}");​
}​
}​
​
Console.WriteLine("Press any key to exit...");​
Console.ReadKey();​
}​
}​
}​
​
​
最近更新时间:2026.01.20 17:16:15
这个页面对您有帮助吗?
有用
有用
无用
无用