最近公司要为新研发的系统使用阿里云日志服务,以便于后期数据的分析与发掘,我很有幸的被选中,对阿里云的日志服务进行了一周的学习与简单的使用。
using System;
using System.Collections.Generic;
using Aliyun.Api.SLS;
using Aliyun.Api.SLS.Request;
using Aliyun.Api.SLS.Response;
using Aliyun.Api.SLS.Data;
namespace netsdksample45
{
class Program
{
static void Main(string[] args)
{
String endpoint = "" ; //选择与上面步骤创建Project所属区域匹配的日志服务Endpoint
String accessKeyId = "" ; //使用你的阿里云访问秘钥AccessKeyId
String accessKeySecret = "" ; //使用你的阿里云访问秘钥AccessKeySecret
String project = "" ; //上面步骤创建的项目名称
String logstore = "" ; //上面步骤创建的日志库名称
//构建一个客户端实例
SLSClient client = new SLSClient(endpoint, accessKeyId, accessKeySecret);
//列出当前Project下的所有日志库名称
ListLogstoresResponse res1 = client.ListLogstores(new ListLogstoresRequest(project));
Console.WriteLine("Totoal logstore number is " + res1.Count);
foreach (String name in res1.Logstores)
{
Console.WriteLine(name);
}
DateTime unixTimestampZeroPoint = new DateTime(1970, 01, 01, 0, 0, 0,DateTimeKind.Utc);
//写入日志
List logs = new List();
for (int i = 0; i < 5; i++)
{
LogItem item = new LogItem();
item.Time = (uint)((DateTime.UtcNow - unixTimestampZeroPoint).TotalSeconds);
item.PushBack("index", i.ToString());
logs.Add(item);
}
String topic = String.Empty; //选择合适的日志主题
String source = "localhost"; //选择合适的日志来源(如IP地址)
PutLogsResponse res4 = client.PutLogs(new PutLogsRequest(project, logstore, topic, source, logs));
Console.WriteLine("Request ID for PutLogs: " + res4.GetRequestId());
//等待1分钟让日志可查询
System.Threading.Thread.Sleep(60 * 1000);
//查询日志分布情况
DateTime fromStamp = DateTime.UtcNow - new TimeSpan(0, 10, 0);
DateTime toStamp = DateTime.UtcNow;
uint from = (uint)((fromStamp - unixTimestampZeroPoint).TotalSeconds);
uint to = (uint)((toStamp - unixTimestampZeroPoint).TotalSeconds);
GetHistogramsResponse res2 = null;
do
{
res2 = client.GetHistograms(new GetHistogramsRequest(project, logstore, from, to));
} while ((res2 != null) && (!res2.IsCompleted()));
Console.WriteLine("Total count of logs is " + res2.TotalCount);
foreach (Histogram ht in res2.Histograms)
{
Console.WriteLine(String.Format("from {0}, to {1}, count {2}.", ht.From, ht.To, ht.Count));
}
//查询日志数据
GetLogsResponse res3 = null;
do
{
res3 = client.GetLogs(new GetLogsRequest(project, logstore, from, to, String.Empty, String.Empty, 5, 0, true));
} while ((res3 != null) && (!res3.IsCompleted()));
Console.WriteLine("Have gotten logs...");
foreach(QueriedLog log in res3.Logs)
{
Console.WriteLine("----{0}, {1}---", log.Time, log.Source);
for(int i = 0; i < log.Contents.Count; i++)
{
Console.WriteLine("{0} --> {1}", log.Contents[i].Key, log.Contents[i].Value);
}
}
}
}
}
本文参考来源,https://www.aliyun.com/product/sls/?spm=5176.8142029.388261.74.JVGhs4
,相关的SDK下载可以到阿里云官网进行下载与查看使用方法。