このトピックでは、Simple Log Service SDK forの使用を開始する方法について説明します。NET Coreを使用して、一般的な操作を実行します。 たとえば、プロジェクトの作成、Logstoreの作成、ログの書き込み、ログの照会ができます。
前提条件
RAM (Resource Access Management) ユーザーが作成され、必要な権限がRAMユーザーに付与されます。 詳細については、「RAMユーザーの作成とRAMユーザーへの権限付与」をご参照ください。
ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数が設定されています。 詳細については、「環境変数の設定」をご参照ください。
重要Alibaba CloudアカウントのAccessKeyペアには、すべてのAPI操作に対する権限があります。 RAMユーザーのAccessKeyペアを使用して、API操作を呼び出したり、ルーチンのO&Mを実行したりすることを推奨します。
プロジェクトコードにAccessKey IDまたはAccessKey secretを保存しないことを推奨します。 そうしないと、AccessKeyペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。
Simple Log Service SDK for。NET Coreがインストールされています。 詳細については、「」をご参照ください。Simple Log Service SDK forをインストールします。NETコア.
サンプルコード
この例では、SLSQuickStart.csという名前のファイルが作成されます。 このファイルのサンプルコードでは、API操作を呼び出してプロジェクトを作成し、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
{
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
private static string endpoint = "cn-hangzhou.log.aliyuncs.com";
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
private static string accessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
private static string accessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
private static string project = "aliyun-test-project";
// The name of the Logstore.
private static string logstore = "aliyun-test-logstore";
// Create a Simple Log Service client.
private static ILogServiceClient client = BuildSimpleClient();
static async Task Main(string[] args)
{
// Create a project.
var proRes = await client.CreateProjectAsync(project, "des");
check(proRes);
Console.WriteLine("Create project success");
Thread.Sleep(120 * 1000);
// Create a Logstore.
var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2);
check(storeRes);
Console.WriteLine("Create logstore success");
Thread.Sleep(10 * 1000);
// Create indexes for the Logstore.
var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}));
check(indRes);
Console.WriteLine("Create Index success");
Thread.Sleep(60 * 1000);
// Write data to the Logstore.
await PostLogs();
Console.WriteLine("Post logs success");
Thread.Sleep(3000);
// Query logs.
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);
}
}
}
}サンプルコードの詳細については、Alibaba Cloud Simple Log Service SDK for。NETコア.
レスポンス
上記の例では、次のレスポンスが返されます。
Create project success
Create logstore success
Create Index success
Post logs success
name : zs
age : 18
address :
__topic__ : test
__source__ : 203.0.XX.XX
__tag__:Tag1 : t1
__tag__:Tag2 :
__tag__:Tag3 : t3
__time__ : 1627970965
======
......