#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using NewLife;
using NewLife.Data;
using NewLife.Security;
using Stardust.Data;
using Stardust.Managers;
using Xunit;
namespace ServerTest.Apps;
/// <summary>应用日志上报端到端测试。客户端读本地日志文件 → 真实 HTTP /Log → 服务端解析落库(收口记录 C-3)</summary>
[Collection(StarTestServerCollection.Name)]
public class AppLogReportE2ETests : StarTestBase
{
public AppLogReportE2ETests(StarTestServer fixture) : base(fixture) { }
private static String CreateTempDir()
{
var dir = Path.Combine(Path.GetTempPath(), "stardust-log-e2e-" + Guid.NewGuid().ToString("n")[..8]);
Directory.CreateDirectory(dir);
return dir;
}
private static IList<AppClientLog> SearchToday(Int32 appId, String? clientId)
=> AppClientLog.Search(appId, clientId, 0, DateTime.Today, DateTime.Today.AddDays(1), null, new PageParameter { PageSize = 100 });
private static IList<AppClientLog> WaitLogs(Int32 appId, String clientId)
{
for (var i = 0; i < 25; i++)
{
var list = SearchToday(appId, clientId);
if (list.Count > 0) return list;
Thread.Sleep(200);
}
return SearchToday(appId, clientId);
}
[Fact(DisplayName = "客户端读取日志尾部并上报,服务端解析入库")]
public async Task ReportTail_PostedAndSaved()
{
var dir = CreateTempDir();
var name = "lpe_" + Rand.NextString(8);
var clientId = "e2e-client@1";
var token = "tok_" + Rand.NextString(16);
var app = new App { Name = name, Secret = "log-secret", Enable = true };
app.Insert();
var online = new AppOnline
{
AppId = app.Id,
Name = app.Name,
Client = clientId,
Token = token,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
};
online.Insert();
try
{
// 当日日志文件:两条标准日志行,客户端按 ResolveLogFile 取当天文件
var file = Path.Combine(dir, $"{DateTime.Now:yyyy_MM_dd}.log");
File.WriteAllText(file, "10:00:01.001 1 Y NewLife.Core first message\r\n10:00:02.002 2 Y NewLife.Core second message\r\n");
var path = AppLogCommands.ResolveLogFile(dir, DateTime.Now);
Assert.Equal(file, path);
var content = AppLogCommands.ReadTail(path, AppLogCommands.DefaultTailBytes);
Assert.NotNull(content);
using var client = _server.CreateClient();
var baseUrl = client.BaseAddress + "";
using var req = AppLogCommands.BuildRequest(baseUrl, name, clientId, token, content!);
using var res = await client.SendAsync(req);
var text = await res.Content.ReadAsStringAsync();
Assert.True(res.IsSuccessStatusCode, $"上报失败:{(Int32)res.StatusCode} {text}");
Assert.Equal(2, AppLogCommands.ParseParsedCount(text));
var list = WaitLogs(app.Id, clientId);
Assert.Equal(2, list.Count);
Assert.Contains(list, e => e.Message.Contains("first message"));
Assert.Contains(list, e => e.Message.Contains("second message"));
}
finally
{
foreach (var item in SearchToday(app.Id, clientId))
{
item.Delete();
}
online.Delete();
app.Delete();
Directory.Delete(dir, true);
}
}
[Fact(DisplayName = "日志尾部按上限截断后只上报末尾内容")]
public async Task ReportTail_TruncatedByMaxBytes()
{
var dir = CreateTempDir();
var name = "lpt_" + Rand.NextString(8);
var clientId = "e2e-client@2";
var token = "tok_" + Rand.NextString(16);
var app = new App { Name = name, Secret = "log-secret", Enable = true };
app.Insert();
var online = new AppOnline
{
AppId = app.Id,
Name = app.Name,
Client = clientId,
Token = token,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
};
online.Insert();
try
{
var file = Path.Combine(dir, $"{DateTime.Now:yyyy_MM_dd}.log");
// 第一行很长,尾部只截到最后一条完整日志行;被截断的半行由服务端按格式自动跳过
File.WriteAllText(file, "10:00:01.001 1 Y NewLife.Core " + new String('x', 300) + "\r\n10:00:03.003 3 Y NewLife.Core tail message\r\n");
var path = AppLogCommands.ResolveLogFile(dir, DateTime.Now);
Assert.NotNull(path);
var content = AppLogCommands.ReadTail(path, 100);
Assert.NotNull(content);
Assert.Contains("tail message", content);
Assert.DoesNotContain(new String('x', 100), content);
using var client = _server.CreateClient();
using var req = AppLogCommands.BuildRequest(client.BaseAddress + "", name, clientId, token, content!);
using var res = await client.SendAsync(req);
var text = await res.Content.ReadAsStringAsync();
Assert.True(res.IsSuccessStatusCode, $"上报失败:{(Int32)res.StatusCode} {text}");
Assert.Equal(1, AppLogCommands.ParseParsedCount(text));
var list = WaitLogs(app.Id, clientId);
Assert.Single(list);
Assert.Contains("tail message", list[0].Message);
}
finally
{
foreach (var item in SearchToday(app.Id, clientId))
{
item.Delete();
}
online.Delete();
app.Delete();
Directory.Delete(dir, true);
}
}
}
|