SLS.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 void PostLogs(string logstore, string Topic, string Source, Dictionary<String, String> Contents, Dictionary<String, String> LogTags)
  82. {
  83. var response = await client.PostLogStoreLogsAsync(logstore, new LogGroupInfo
  84. {
  85. Topic = Topic,
  86. Source = Source,
  87. LogTags = LogTags,
  88. Logs = new List<LogInfo>
  89. {
  90. new LogInfo
  91. {
  92. Time = DateTimeOffset.Now,
  93. Contents = Contents
  94. // new Dictionary<String, String>
  95. // {
  96. // {"name", "zs"},
  97. // {"age", "18"},
  98. // {"address", String.Empty}
  99. // }
  100. }
  101. }
  102. });
  103. check(response);
  104. }
  105. public static ILogServiceClient BuildSimpleClient()
  106. => LogServiceClientBuilders.HttpBuilder
  107. .Endpoint(endpoint, project)
  108. .Credential(accessKeyId, accessKeySecret)
  109. .Build();
  110. public static void check(IResponse res)
  111. {
  112. if (!res.IsSuccess)
  113. {
  114. throw new ApplicationException(res.Error.ErrorMessage);
  115. }
  116. }
  117. /// <summary>
  118. /// SLS日志基础方法
  119. /// </summary>
  120. /// <param name="createDate"></param>
  121. /// <param name="topic"></param>
  122. /// <param name="content"></param>
  123. /// <param name="otherTags"></param>
  124. /// <returns></returns>
  125. public static string WriteLog(DateTime createDate, string topic, string content, Dictionary<string, string> otherTags)
  126. {
  127. Dictionary<string, string> tags = new Dictionary<string, string>();
  128. tags.Add("datetime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  129. tags.Add("date", DateTime.Now.ToString("yyyy-MM-dd"));
  130. tags.Add("month", DateTime.Now.ToString("yyyy-MM"));
  131. tags.Add("year", DateTime.Now.ToString("yyyy"));
  132. foreach(string key in otherTags.Keys)
  133. {
  134. tags.Add(key, otherTags[key]);
  135. }
  136. SLS.PostLogs("spserver", topic, "47.108.229.115", new Dictionary<string, string>{
  137. {"content", content}
  138. }, tags);
  139. return "ok";
  140. }
  141. }
  142. }