mac
2024-07-25 3f6685c77beeb12baf840733fb890860f4c26e7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using 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";
        // 本示例从环境变量中获取AccessKey ID和AccessKey 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);
 
        //    // 为Logstore创建索引。
        //    var indRes = await client.CreateIndexAsync(logstore, new IndexLineInfo(new[] { ' ', ',' }));
        //    check(indRes);
        //    Console.WriteLine("Create Index success");
        //    Thread.Sleep(60 * 1000);
 
        //    // 向Logstore写入数据。
        //    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);
            }
        }
    }
}