SLS.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Aliyun.Api.LogService;
  6. using Aliyun.Api.LogService.Domain.Log;
  7. using Aliyun.Api.LogService.Domain.LogStore.Index;
  8. using Aliyun.Api.LogService.Infrastructure.Protocol;
  9. namespace MySystem
  10. {
  11. public class SLS
  12. {
  13. //配置AccessKey、服务入口、Project名称、Logstore名称等相关信息。
  14. //日志服务的服务入口。更多信息,请参见服务入口。
  15. //此处以杭州为例,其它地域请根据实际情况填写。
  16. private static string endpoint = "cn-chengdu.log.aliyuncs.com";
  17. //阿里云访问密钥AccessKey。更多信息,请参见访问密钥。阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
  18. private static string accessKeyId = "LTAI5t9L1WAyzPaQzX57kSxG";
  19. private static string accessKeySecret = "BqdskHlgU402kHhlpvdTOLsQZ63xKw";
  20. //Project名称。
  21. private static string project = "kexiaoshuang";
  22. //Logstore名称。
  23. private static string logstore = "aliyun-test-logstore";
  24. //创建日志服务Client。
  25. private static ILogServiceClient client = BuildSimpleClient();
  26. static async Task Main()
  27. {
  28. //创建Project。
  29. var proRes = await client.CreateProjectAsync(project, "des");
  30. check(proRes);
  31. Console.WriteLine("Create project success");
  32. Thread.Sleep(120 * 1000);
  33. //创建Logstore。
  34. var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2);
  35. check(storeRes);
  36. Console.WriteLine("Create logstore success");
  37. Thread.Sleep(10 * 1000);
  38. //为Logstore创建索引。
  39. var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}));
  40. check(indRes);
  41. Console.WriteLine("Create Index success");
  42. Thread.Sleep(60 * 1000);
  43. //向Logstore写入数据。
  44. // await PostLogs();
  45. Console.WriteLine("Post logs success");
  46. Thread.Sleep(3000);
  47. //查询日志。
  48. await GetLogs();
  49. }
  50. public static async void CreateProject(string name, string detail)
  51. {
  52. var proRes = await client.CreateProjectAsync(name, detail);
  53. check(proRes);
  54. }
  55. public static async void CreateLogStore(string logstore)
  56. {
  57. var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2, project);
  58. check(storeRes);
  59. }
  60. public static async void CreateIndex(string logstore)
  61. {
  62. var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] {' ', ','}), project);
  63. check(indRes);
  64. }
  65. public static async Task GetLogs()
  66. {
  67. var logsRes = await client.GetLogsAsync(logstore, DateTimeOffset.UtcNow.AddMinutes(-1),
  68. DateTimeOffset.UtcNow,
  69. "test", "", 100, 0);
  70. check(logsRes);
  71. foreach (var log in logsRes.Result.Logs)
  72. {
  73. foreach (var key in log.Keys)
  74. {
  75. log.TryGetValue(key, out var value);
  76. Console.WriteLine(key + " : " + value);
  77. }
  78. Console.WriteLine("======");
  79. }
  80. }
  81. public static async Task<IList<IDictionary<string, string>>> GetLogs(string logstore, string topic, string query, DateTimeOffset from, int offset = 0, int line = 50)
  82. {
  83. var logsRes = await client.GetLogsAsync(logstore, from,
  84. DateTimeOffset.UtcNow,
  85. topic, query, line, offset);
  86. check(logsRes);
  87. return logsRes.Result.Logs;
  88. }
  89. public static async void PostLogs(string logstore, string Topic, string Source, Dictionary<String, String> Contents, Dictionary<String, String> LogTags)
  90. {
  91. var response = await client.PostLogStoreLogsAsync(logstore, new LogGroupInfo
  92. {
  93. Topic = Topic,
  94. Source = Source,
  95. LogTags = LogTags,
  96. Logs = new List<LogInfo>
  97. {
  98. new LogInfo
  99. {
  100. Time = DateTimeOffset.Now,
  101. Contents = Contents
  102. // new Dictionary<String, String>
  103. // {
  104. // {"name", "zs"},
  105. // {"age", "18"},
  106. // {"address", String.Empty}
  107. // }
  108. }
  109. }
  110. });
  111. check(response);
  112. }
  113. public static ILogServiceClient BuildSimpleClient()
  114. => LogServiceClientBuilders.HttpBuilder
  115. .Endpoint(endpoint, project)
  116. .Credential(accessKeyId, accessKeySecret)
  117. .Build();
  118. public static void check(IResponse res)
  119. {
  120. if (!res.IsSuccess)
  121. {
  122. throw new ApplicationException(res.Error.ErrorMessage);
  123. }
  124. }
  125. /// <summary>
  126. /// SLS日志基础方法
  127. /// </summary>
  128. /// <param name="createDate"></param>
  129. /// <param name="topic"></param>
  130. /// <param name="content"></param>
  131. /// <param name="otherTags"></param>
  132. /// <returns></returns>
  133. public static string WriteLog(DateTime createDate, string topic, string content, Dictionary<string, string> otherTags)
  134. {
  135. Dictionary<string, string> tags = new Dictionary<string, string>();
  136. tags.Add("datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  137. tags.Add("date", DateTime.Now.ToString("yyyy-MM-dd"));
  138. tags.Add("month", DateTime.Now.ToString("yyyy-MM"));
  139. tags.Add("year", DateTime.Now.ToString("yyyy"));
  140. foreach(string key in otherTags.Keys)
  141. {
  142. tags.Add(key, otherTags[key]);
  143. }
  144. SLS.PostLogs("spserver", topic, "47.108.229.115", new Dictionary<string, string>{
  145. {"content", content}
  146. }, tags);
  147. return "ok";
  148. }
  149. }
  150. }