From c2f74dd7b1df9829385c8fa7fcff860f6a5aae1f Mon Sep 17 00:00:00 2001 From: wxr <wxr@hdlchina.com.cn> Date: 星期五, 12 七月 2024 14:23:40 +0800 Subject: [PATCH] Merge branch 'Wxr-UnuitTest' into Dev_v2.4.8_google --- HDL_ON/Common/AliyunLog/AliyunLogClient.cs | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 160 insertions(+), 0 deletions(-) diff --git a/HDL_ON/Common/AliyunLog/AliyunLogClient.cs b/HDL_ON/Common/AliyunLog/AliyunLogClient.cs new file mode 100644 index 0000000..02c0336 --- /dev/null +++ b/HDL_ON/Common/AliyunLog/AliyunLogClient.cs @@ -0,0 +1,160 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Aliyun.Api.LogService; +using Aliyun.Api.LogService.Domain.Log; +using Aliyun.Api.LogService.Domain.LogStore.Index; +using Aliyun.Api.LogService.Infrastructure.Protocol; + +namespace HDL_ON.Common.AliyunLog +{ + public class AliyunLogClient + { + // 鏃ュ織鏈嶅姟鐨勬湇鍔℃帴鍏ョ偣銆傛澶勪互鏉窞涓轰緥锛屽叾瀹冨湴鍩熻鏍规嵁瀹為檯鎯呭喌濉啓銆� + private static string endpoint = "cn-hangzhou.log.aliyuncs.com"; + // 鏈ず渚嬩粠鐜鍙橀噺涓幏鍙朅ccessKey ID鍜孉ccessKey Secret銆� + private static string accessKeyId = "LTAI5tLzDxrtsFyi3xtK3YWt"; + private static string accessKeySecret = "eX31JZrRAvC2wZWPiZU0SYhlfAUMoT"; + // Project鍚嶇О銆� + private static string project = "hdl-onpro-log"; + // Logstore鍚嶇О銆� + private static string logstore = "log"; + // 鍒涘缓鏃ュ織鏈嶅姟Client銆� + private static ILogServiceClient client = BuildSimpleClient(); + + public static ILogServiceClient BuildSimpleClient() + => LogServiceClientBuilders.HttpBuilder + .Endpoint(endpoint, project) + .Credential(accessKeyId, accessKeySecret) + .Build(); + + + public AliyunLogClient() + { + } + + + //// 鏋勫缓鏈�绠�鍗曠殑`ILogServiceClient`銆� + //public static ILogServiceClient BuildSimpleClient() + // => LogServiceClientBuilders.HttpBuilder + // // 鏈嶅姟鍏ュ彛<endpoint>鍙婇」鐩悕<projectName> + // .Endpoint(endpoint, project) + // // 璁块棶瀵嗛挜淇℃伅 + // .Credential(accessKeyId, accessKeySecret) + // .Build(); + + + //static async Task Main(string[] args) + //{ + // // 鍒涘缓Project銆� + // var proRes = await client.CreateProjectAsync(project, "des"); + // check(proRes); + // Console.WriteLine("Create project success"); + // Thread.Sleep(120 * 1000); + + // // 鍒涘缓Logstore銆� + // var storeRes = await client.CreateLogStoreAsync(logstore, 3, 2); + // check(storeRes); + // Console.WriteLine("Create logstore success"); + // Thread.Sleep(10 * 1000); + + // // 涓篖ogstore鍒涘缓绱㈠紩銆� + // var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] { ' ', ',' })); + // check(indRes); + // Console.WriteLine("Create Index success"); + // Thread.Sleep(60 * 1000); + + // // 鍚慙ogstore鍐欏叆鏁版嵁銆� + // await PostLogs(); + // Console.WriteLine("Post logs success"); + // Thread.Sleep(3000); + // // 鏌ヨ鏃ュ織銆� + // await GetLogs(); + //} + + //public static async Task GetLogs() + //{ + // var logsRes = await client.GetLogsAsync(logstore, DateTimeOffset.UtcNow.AddMinutes(-1), + // DateTimeOffset.UtcNow, + // "test", "", 100, 0); + // check(logsRes); + // foreach (var log in logsRes.Result.Logs) + // { + // foreach (var key in log.Keys) + // { + // log.TryGetValue(key, out var value); + // Console.WriteLine(key + " : " + value); + // } + + // Console.WriteLine("======"); + // } + //} + + + /// <summary> + /// 鏃ュ織鎺ㄩ�� + /// </summary> + /// <param name="topic">鎺ㄩ�佹爣棰�</param> + /// <param name="msg">鎺ㄩ�佷俊鎭�</param> + /// <returns></returns> + public static async Task PostLogs(string topic, string msg) + { + if (client == null) + { + client = BuildSimpleClient(); + } + + try + { + var pushObj = new LogGroupInfo + { + Topic = topic, +#if __ANDROID__ + Source = "Android", +#else + Source = "IOS", +#endif + LogTags = new Dictionary<string, string> + { + {"tag1", DateTime.Now.ToLongTimeString()}, + + }, + Logs = new List<LogInfo> + { + new LogInfo + { + Time = DateTimeOffset.Now, + Contents = new Dictionary<string, string> + { + {"id", DateTime.Now.Ticks.ToString()}, + {"homeid", Entity.DB_ResidenceData.Instance.CurrentRegion.id}, + {"userid", UserInfo.Current.ID}, + {"userName", UserInfo.Current.userName}, + {"server", DAL.Server.HttpUtil.GlobalRequestHttpsHost}, + {"message", msg}, + } + } + } + }; + var response = await client.PostLogStoreLogsAsync(logstore, pushObj); + check(response); + } + catch (Exception ex) + { + MainPage.Log(ex.Message); + } + } + + + public static void check(IResponse res) + { + if (!res.IsSuccess) + { + MainPage.Log("鏃ュ織鎺ㄩ�佷笉鎴愬姛."); + //throw new ApplicationException(res.Error.ErrorMessage); + } + } + } +} + -- Gitblit v1.8.0