This topic describes how to get started with Log Service SDK for .NET Core and perform common operations. For example, you can create a project, create a Logstore, write logs, and query logs.

Prerequisites

Log Service SDK for .NET Core is installed. For more information, see Install Log Service SDK for .NET Core.

Sample code

In this example, the SLSQuickStart.cs file is created. The sample code in this file provides an example on how to call API operations to create a project, create a Logstore, create indexes, write logs, and query logs. Example:
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
    {
        // Configure information such as the AccessKey pair, endpoint, project name, and Logstore name. 
        // The endpoint of Log Service. For more information, see Endpoints. 
        // In this example, the Log Service endpoint for the China (Hangzhou) region is used. You must replace this value with an actual endpoint based on your business requirements. 
        private static string endpoint = "cn-hangzhou.log.aliyuncs.com";
        // The AccessKey pair used to access Log Service. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of an Alibaba Cloud account, security risks may occur. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
        private static string accessKeyId = "your_access_id";
        private static string accessKeySecret = "your_access_key";
        // 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 client for Log Service. 
        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);
            }
        }
    }
}

For more information about sample codes, see aliyun-log-dotnetcore-sdk.

Response

In this example, the following result is returned:

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
======
......