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);
|
}
|
}
|
}
|
}
|