本文介绍如何快速使用日志服务.NET Core SDK完成常见操作,包括创建项目(Project)、创建日志库(Logstore)、写入日志和查询日志等。
前提条件
示例代码
本示例中,创建一个SLSQuickStart.cs文件,并调用接口分别完成创建Project、创建Logstore、创建索引、写入日志数据和查询日志数据。示例如下:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Domain.Log;
using Aliyun.Api.LogService.Domain.LogStore.Index;
using Aliyun.Api.LogService.Infrastructure.Protocol;
namespace Test
{
class SLSQuickStart
{
//配置AccessKey、服务入口、Project名称、Logstore名称等相关信息。
//日志服务的服务入口。更多信息,请参见服务入口。
//此处以杭州为例,其它地域请根据实际情况填写。
private static string endpoint = "cn-hangzhou.log.aliyuncs.com";
//阿里云访问密钥AccessKey。更多信息,请参见访问密钥。阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
private static string accessKeyId = "your_access_id";
private static string accessKeySecret = "your_access_key";
//Project名称。
private static string project = "aliyun-test-project";
//Logstore名称。
private static string logstore = "aliyun-test-logstore";
//创建日志服务Client。
private static ILogServiceClient client = BuildSimpleClient();
static async Task Main(string[] args)
{
//创建Project。
var proRes = await client.CreateProjectAsync(project, "des");
check(proRes);
Console.WriteLine("Create project success");
Thread.Sleep(120 * 1000);
//创建Logstore。
var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2);
check(storeRes);
Console.WriteLine("Create logstore success");
Thread.Sleep(10 * 1000);
//为Logstore创建索引。
var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}));
check(indRes);
Console.WriteLine("Create Index success");
Thread.Sleep(60 * 1000);
//向Logstore写入数据。
await PostLogs();
Console.WriteLine("Post logs success");
Thread.Sleep(3000);
//查询日志。
await GetLogs();
}
public static async Task GetLogs()
{
var logsRes = await client.GetLogsAsync(logstore, DateTimeOffset.UtcNow.AddMinutes(-1),
DateTimeOffset.UtcNow,
"test", "", 100, 0);
check(logsRes);
foreach (var log in logsRes.Result.Logs)
{
foreach (var key in log.Keys)
{
log.TryGetValue(key, out var value);
Console.WriteLine(key + " : " + value);
}
Console.WriteLine("======");
}
}
public static async Task PostLogs()
{
for (int i = 0; i < 10; i++)
{
var response = await client.PostLogStoreLogsAsync(logstore, new LogGroupInfo
{
Topic = "test",
Source = "49.111.66.122",
LogTags = new Dictionary<String, String>
{
{"Tag1", "t1"},
{"Tag2", String.Empty},
{"Tag3", "t3"}
},
Logs = new List<LogInfo>
{
new LogInfo
{
Time = DateTimeOffset.Now,
Contents = new Dictionary<String, String>
{
{"name", "zs"},
{"age", "18"},
{"address", String.Empty}
}
}
}
});
check(response);
}
}
public static ILogServiceClient BuildSimpleClient()
=> LogServiceClientBuilders.HttpBuilder
.Endpoint(endpoint, project)
.Credential(accessKeyId, accessKeySecret)
.Build();
public static void check(IResponse res)
{
if (!res.IsSuccess)
{
throw new ApplicationException(res.Error.ErrorMessage);
}
}
}
}
更多示例代码,请参见Aliyun Log .NET Core SDK。
返回结果
返回结果示例如下:
Create project success
Create logstore success
Create Index success
Post logs success
name : zs
age : 18
address :
__topic__ : test
__source__ : 203.0.112.10
__tag__:Tag1 : t1
__tag__:Tag2 :
__tag__:Tag3 : t3
__time__ : 1627970965
======
......