阿里云日志服务NetCore SDK Quick Start

Step By Step

1、创建Net Core 控制台项目
2、安装SLS NET Core SDK
3、编写发送日志到服务端代码
4、测试查看效果


一、创建Net Core 控制台项目

阿里云日志服务NetCore SDK Quick Start

阿里云日志服务NetCore SDK Quick Start

阿里云日志服务NetCore SDK Quick Start

二、安装SLS NET Core SDK

阿里云日志服务NetCore SDK Quick Start

阿里云日志服务NetCore SDK Quick Start

阿里云日志服务NetCore SDK Quick Start

三、编写发送日志到服务端代码
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Domain.Log;
using Aliyun.Api.LogService.Infrastructure.Protocol.Http;
using System;
using System.Collections.Generic;

namespace slsdemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connection configuration information
            string projectName = "******";
            string logstoreName = "******";
            string accessKeyId = "LTAI5t7f****************";
            string accessSecret = "R2DOjvYe80Sbux****************";
            string endpoint = "http://<ProjectName>.cn-<RegionId>.log.aliyuncs.com";

            HttpLogServiceClient client = LogServiceClientBuilders.HttpBuilder.Endpoint(endpoint, projectName).Credential(accessKeyId, accessSecret).Build();

            // raw logs
            var logInfo1 = new LogInfo
            {
                Contents =
                        {
                            {"level", "info"},
                            {"id", "1"},
                            {"status", "200"}
                        },
                Time = DateTimeOffset.Now
            };

            var logInfo2 = new LogInfo
            {
                Contents =
                {
                    { "level", "error" },
                    { "id", "2" },
                    { "status", "400" }
                },
                Time = DateTimeOffset.Now.AddSeconds(1)
            };

            // Create a list of LogInfo
            List<LogInfo> parts = new List<LogInfo>();
            parts.Add(logInfo1);
            parts.Add(logInfo2);

            // create logGroupInfo
            var logGroupInfo = new LogGroupInfo
            {
                Topic = "example",
                Source = "sls-source",
                LogTags =
                {
                    {"example", "true"},
                },
                Logs = parts
            };

            // send logs to sls
            PostLogsRequest request = new PostLogsRequest(logstoreName, logGroupInfo);

            var response = client.PostLogStoreLogsAsync(request).GetAwaiter().GetResult();

            Console.WriteLine("response: " + response.IsSuccess);
        }
    }
}
四、测试查看效果

阿里云日志服务NetCore SDK Quick Start

阿里云日志服务NetCore SDK Quick Start


更多参考

PostLogStoreLogsExample.cs
System.ArgumentNullException:“Value cannot be null. Arg_ParamName_Name”

上一篇:DockOne微信分享(六十八):应用容器env化实战


下一篇:物联网网关市场预计到2026年将增长21.5亿美元