mac
2024-07-25 16bea1d248f0010049bceaa562939297fa26b130
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
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Linq;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Domain;
using Aliyun.Api.LogService.Domain.Log;
using Aliyun.Api.LogService.Infrastructure.Protocol;
 
namespace HDL_ON.Common.AliyunLog
{
    public class AliyunLogInvokeAsync
    {
        public AliyunLogInvokeAsync()
        {
        }
 
        /// <summary>
        /// 执行请求方法。
        /// </summary>
        public static async Task<GetLogsResult> Invoke(ILogServiceClient client)
        {
            var response = await client.GetLogsAsync
            (
                // 「必填参数」会在 Request 构造器中列出,并且不可set;
                new GetLogsRequest("example-logstore", (Int32)DateTimeOffset.UtcNow.AddDays(-1).ToUnixTimeSeconds(), (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds())
                {
                    // 「可选参数」不会在 Request 构造器中列出,可通过setter设置。
                    Offset = 1,
                    Line = 100,
                }
            );
 
            var result = response
                // 此方法会确保返回的响应失败时候抛出`LogServiceException`。
                .EnsureSuccess()
                // 此处获取Result是安全的。
                .Result;
 
            Console.WriteLine($"RequestId:{response.RequestId}");
            Console.WriteLine($"日志总数:{result.Count}");
            Console.WriteLine($"首条日志:{result.Logs.FirstOrDefault()}");
 
            return result;
        }
 
        /// <summary>
        /// 在调用时可使用扩展方法,扩展方法会将简单的请求对象的属性展开到方法入参中。
        /// 使用扩展方法 `using Aliyun.Api.Log;` 即可。
        /// </summary>
        public static async Task<GetLogsResult> InvokeUsingExtension(ILogServiceClient client)
        {
            var response = await client.GetLogsAsync
            (
                // 「必填参数」作为方法的普通必须参数
                "example-logstore",
                DateTimeOffset.UtcNow.AddDays(-1),
                DateTimeOffset.UtcNow,
 
                // 「可选参数」作为方法的可选参数,可通过命名参数方式指定
                offset: 1,
                line: 10
            );
 
            var result = response
                // 此方法会确保返回的响应失败时候抛出`LogServiceException`。
                .EnsureSuccess()
                // 此处获取Result是安全的。
                .Result;
 
            Console.WriteLine($"RequestId:{response.RequestId}");
            Console.WriteLine($"日志总数:{result.Count}");
            Console.WriteLine($"首条日志:{result.Logs.FirstOrDefault()}");
 
            return result;
        }
 
        /// <summary>
        /// 处理服务器返回(包含在Response中)的错误。
        /// </summary>
        public static async Task<GetLogsResult> InvokeWithErrorHandling(ILogServiceClient client)
        {
            var response = await client.GetLogsAsync
            (
                // 「必填参数」作为方法的普通必须参数
                "example-logstore",
                DateTimeOffset.UtcNow.AddDays(-1),
                DateTimeOffset.UtcNow,
 
                // 「可选参数」作为方法的可选参数,可通过命名参数方式指定
                offset: 1,
                line: 10
            );
 
            GetLogsResult result;
            // 尝试处理可处理的错误。
            if (!response.IsSuccess)
            {
                // 错误码
                var errorCode = response.Error.ErrorCode;
                // 错误消息
                var errorMessage = response.Error.ErrorMessage;
 
                Console.WriteLine($"RequestId:{response.RequestId}");
                Console.WriteLine($"错误码:{errorCode}");
                Console.WriteLine($"错误信息:{errorMessage}");
 
                // `ErrorCode`类可支持与自身实例或字符串进行对比。
                if (errorCode == ErrorCode.SignatureNotMatch /* SDK中预定义的错误码 */)
                {
                    // 在这里处理业务可处理的错误。。。。。。
                    Console.WriteLine("Signature not match, {0}.", errorMessage);
                }
                else if (errorCode == "ParameterInvalid" /* 业务相关特殊的SDK中未定义的错误码 */)
                {
                    // 在这里处理业务可处理的错误。。。。。。
                    Console.WriteLine("Parameter invalid, {0}.", errorMessage);
                }
 
                // 任何处理不到的错误请务必抛出异常中断原流程,避免外部获取到 null 的结果!
                throw new Exception("这里可以是系统的业务异常。" + response.Error /* 最好带上服务返回的错误信息以便调试 */);
            }
            else
            {
                // 此处获取Result是安全的。
                result = response.Result;
            }
 
            Console.WriteLine($"RequestId:{response.RequestId}");
            Console.WriteLine($"日志总数:{result.Count}");
            Console.WriteLine($"首条日志:{result.Logs.FirstOrDefault()}");
 
            return result;
        }
 
        /// <summary>
        /// 处理以异常形式抛出的错误。
        /// </summary>
        public static async Task<GetLogsResult> InvokeWithExceptionHandling(ILogServiceClient client)
        {
            try
            {
                return await Invoke(client);
            }
            catch (LogServiceException e)
            {
                // 错误码
                var errorCode = e.ErrorCode;
                // 错误消息
                var errorMessage = e.ErrorMessage;
 
                Console.WriteLine($"RequestId:{e.RequestId}");
                Console.WriteLine($"错误码:{errorCode}");
                Console.WriteLine($"错误信息:{errorMessage}");
 
                // `ErrorCode`类可支持与自身实例或字符串进行对比。
                if (errorCode == ErrorCode.SignatureNotMatch /* SDK中预定义的错误码 */)
                {
                    // 在这里处理业务可处理的错误。。。。。。
                    Console.WriteLine("Signature not match, {0}.", errorMessage);
                }
                else if (errorCode == "ParameterInvalid" /* 业务相关特殊的SDK中未定义的错误码 */)
                {
                    // 在这里处理业务可处理的错误。。。。。。
                    Console.WriteLine("Parameter invalid, {0}.", errorMessage);
                }
 
                // 任何处理不到的错误请务必抛出异常中断原流程,避免外部获取到 null 的结果!
                throw new Exception("这里可以是系统的业务异常。", e /* 在自定义的异常中最好带上服务返回的异常以便调试 */);
            }
        }
    }
}