private void GetAliyunLog() { LogClient _client = new LogClient(endpoint, accesskey, accessecret); StringBuilder sb = new StringBuilder(); DateTime unixTimestampZeroPoint = new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc); DateTime fromStamp = DateTime.UtcNow.AddDays(-1); DateTime toStamp = DateTime.UtcNow; sb.Append("beginTime:" + fromStamp + "--" + "endTime" + toStamp + "\n\n"); uint from = (uint)((fromStamp - unixTimestampZeroPoint).TotalSeconds); uint to = (uint)((toStamp - unixTimestampZeroPoint).TotalSeconds); try { GetLogsRequest req = new GetLogsRequest(projectName, logstore, from, to, "", "ActivityLogTypeName:网站商品PV | select ActivityLogTypeName, Comment, count(1) as count group by Comment,ActivityLogTypeName order by count desc limit 100", int.MaxValue, 0, true); GetLogsResponse res = _client.GetLogs(req); if (res != null && res.IsCompleted()) { foreach (QueriedLog log in res.Logs) { foreach (var item in log.Contents) { sb.Append(item.Key + ";" + item.Value + "\n"); } } } MessageBox.Show(sb.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private static void Test2() { string endpoint = "http://cn-shenzhen.log.aliyuncs.com"; //选择与上面步骤创建 project 所属区域匹配的日志服务 Endpoint string accessKeyId = "LTAIAEMVSMtDaRcn"; //使用你的阿里云访问秘钥 AccessKeyId string accessKeySecret = "u6CgiFiWd6ahL8ux4fRd7tWmiHmDhH"; //使用你的阿里云访问秘钥 AccessKeySecret string project = "henry-hyh-logservice"; //上面步骤创建的项目名称 string logstore = "henry-hyh-logstore"; //上面步骤创建的日志库名称 //构建一个客户端实例 LogClient client = new LogClient(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 <LogItem> logs = new List <LogItem>(); for (int i = 0; i < 5; i++) { LogItem item = new LogItem(); item.Time = (uint)((DateTime.UtcNow - unixTimestampZeroPoint).TotalSeconds); item.PushBack("CTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", System.Globalization.DateTimeFormatInfo.InvariantInfo)); item.PushBack("Index", i.ToString()); logs.Add(item); } string topic = "hello.topic"; //选择合适的日志主题 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); System.Threading.Thread.Sleep(5 * 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); var query = "Index"; //查询日志分布情况 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, query, 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); } } Console.WriteLine("Finish"); Console.ReadKey(); }
static void Main(string[] args) { String endpoint = "cn-hangzhou.log.aliyuncs.com"; //选择与上面步骤创建 project 所属区域匹配的日志服务 Endpoint String accessKeyId = "yacNT1obW34STZCL"; //使用你的阿里云访问秘钥 AccessKeyId String accessKeySecret = "K4JY48aALclGK8iZ082B4MPLLfy2BN"; //使用你的阿里云访问秘钥 AccessKeySecret String project = "chekongbao"; //上面步骤创建的项目名称 String logstore = "ckb_log"; //上面步骤创建的日志库名称 //构建一个客户端实例 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 <LogItem> logs = new List <LogItem>(); 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); } } Console.ReadKey(); }