Step By Step
1、创建Net Core 控制台项目
2、安装SLS NET Core SDK
3、编写发送日志到服务端代码
4、测试查看效果
一、创建Net Core 控制台项目
二、安装SLS NET Core SDK
三、编写发送日志到服务端代码
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);
}
}
}
四、测试查看效果
更多参考
PostLogStoreLogsExample.cs
System.ArgumentNullException:“Value cannot be null. Arg_ParamName_Name”