Use Simple Log Service SDK for .NET Core to create a project, create a Logstore, write logs, and query logs.
Prerequisites
-
A RAM user is created and granted the required permissions. For more information, see Create and authorize a RAM User.
-
The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables on Linux, macOS, and Windows.
ImportantAn Alibaba Cloud account's AccessKey grants full API access. For security purposes, use a RAM user's AccessKey for API calls and daily operations.
Do not hard-code your AccessKey ID or AccessKey Secret in project code. Leaked credentials can compromise all resources in your account.
-
Simple Log Service SDK for .NET Core is installed. For more information, see Install Simple Log Service SDK for .NET Core.
Sample code
Create a file named SLSQuickStart.cs and call API operations to create a project, create a Logstore, create indexes, write logs, and query logs.
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. This example uses the China (Hangzhou) endpoint. Replace the value with the endpoint for your region.
private static string endpoint = "cn-hangzhou.log.aliyuncs.com";
// This example retrieves the AccessKey ID and AccessKey Secret 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 an index 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 = "127.0.0.1",
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);
}
}
}
}
For more sample code, see Alibaba Cloud Simple Log Service SDK for .NET Core.
Expected output
After the code runs, the following output is returned:
Create project success
Create logstore success
Create Index success
Post logs success
name : zs
age : 18
address :
__topic__ : test
__source__ : 127.0.0.1
__tag__:Tag1 : t1
__tag__:Tag2 :
__tag__:Tag3 : t3
__time__ : 1721728435
======
......