116 lines
3.3 KiB
C#
116 lines
3.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http.Json;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using ColdMint.scripts.debug;
|
|||
|
using Godot;
|
|||
|
using HttpClient = System.Net.Http.HttpClient;
|
|||
|
|
|||
|
namespace ColdMint.scripts.openObserve;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>LogCollector</para>
|
|||
|
/// <para>日志收集器</para>
|
|||
|
/// </summary>
|
|||
|
public static class LogCollector
|
|||
|
{
|
|||
|
private static readonly List<LogData> _logDataList = [];
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>Automatic upload threshold</para>
|
|||
|
/// <para>自动上传的阈值</para>
|
|||
|
/// </summary>
|
|||
|
/// <remarks>
|
|||
|
///<para>An attempt is made to upload logs when messages reach this number.</para>
|
|||
|
///<para>当消息到达此数量后将尝试上传日志。</para>
|
|||
|
/// </remarks>
|
|||
|
public static int UploadThreshold { get; set; } = 50;
|
|||
|
|
|||
|
private static bool lockList = false;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>httpClient</para>
|
|||
|
/// <para>Http客户</para>
|
|||
|
/// </summary>
|
|||
|
private static HttpClient? _httpClient;
|
|||
|
|
|||
|
private static string? _orgId;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>UpdateHttpClient</para>
|
|||
|
/// <para>更新Http客户端</para>
|
|||
|
/// </summary>
|
|||
|
/// <param name="openObserve"></param>
|
|||
|
public static void UpdateHttpClient(OpenObserve openObserve)
|
|||
|
{
|
|||
|
if (openObserve.Address == null || openObserve.AccessToken == null || openObserve.OrgId == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var httpClient = new HttpClient();
|
|||
|
httpClient.BaseAddress = new Uri(openObserve.Address);
|
|||
|
//Add a Cookie to the request header
|
|||
|
//添加Cookie到请求头
|
|||
|
var cookie = new Cookie("auth_tokens",
|
|||
|
"{\"access_token\":\"Basic " + openObserve.AccessToken + "\",\"refresh_token\":\"\"}");
|
|||
|
httpClient.DefaultRequestHeaders.Add("Cookie", cookie.ToString());
|
|||
|
_httpClient = httpClient;
|
|||
|
_orgId = openObserve.OrgId;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>Push log</para>
|
|||
|
/// <para>推送日志</para>
|
|||
|
/// </summary>
|
|||
|
/// <param name="logRequestBean"></param>
|
|||
|
private static async Task PostLog(List<LogData> logRequestBean)
|
|||
|
{
|
|||
|
if (_httpClient == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
lockList = true;
|
|||
|
GD.Print("开始上传");
|
|||
|
var httpResponseMessage = await _httpClient.PostAsJsonAsync("/api/" + _orgId + "/game/_json", logRequestBean);
|
|||
|
if (httpResponseMessage.IsSuccessStatusCode)
|
|||
|
{
|
|||
|
_logDataList.RemoveRange(0, logRequestBean.Count);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
GD.Print("上传失败" + httpResponseMessage.StatusCode.ToString());
|
|||
|
}
|
|||
|
|
|||
|
lockList = false;
|
|||
|
}
|
|||
|
|
|||
|
public static void Push(LogData logData)
|
|||
|
{
|
|||
|
_logDataList.Add(logData);
|
|||
|
GD.Print("上传吗" + lockList + "长度为" + _logDataList.Count + "阈值" + UploadThreshold);
|
|||
|
if (!lockList && _logDataList.Count > UploadThreshold)
|
|||
|
{
|
|||
|
//执行上传
|
|||
|
GD.Print("上传");
|
|||
|
PostLog(_logDataList.GetRange(0, UploadThreshold));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class LogData
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// <para>The AppId of this application</para>
|
|||
|
/// <para>此应用的AppId</para>
|
|||
|
/// </summary>
|
|||
|
public string? AppId { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <para>message</para>
|
|||
|
/// <para>消息</para>
|
|||
|
/// </summary>
|
|||
|
public string? Message { get; set; }
|
|||
|
}
|