LogPage.razor
1.64 KB
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
@page "/"
@using AntDesign
@using DataAcquisition.Common.Utils
@using Microsoft.Extensions.Caching.Memory
@using System.Collections.Concurrent
<Card Title="日志" Size="small">
@if (!Logs.Any())
{
<Empty />
}
else
{
<Virtualize Items="@Logs.OrderByDescending(x => x.DateTime).ToList()" Context="log">
<Alert Message="@($"[{log.DateTime}]:{log.Message}")" Type="@log.StatusCode.ToString().ToLower()" ShowIcon="true" />
</Virtualize>
}
</Card>
@code
{
private SystemLog log = SystemLog.Instance;
private static List<LogVM> Logs = new List<LogVM>();
protected override void OnInitialized()
{
InitialLog();
base.OnInitialized();
}
private void InitialLog()
{
_ = InvokeAsync(async () =>
{
int max = 30;
while (true)
{
while (!log.IsEmpty)
{
var newItem = log.GetLog();
if (newItem == null) continue;
var oldItem = Logs.Where(x => x.Message == newItem.Message).FirstOrDefault();
if (oldItem != null)
{
oldItem.DateTime = newItem.DateTime;
}
else
{
Logs.Add(newItem);
}
}
if (Logs.Count > max)
{
Logs = Logs.OrderByDescending(x => x.DateTime).Take(max).ToList();
}
StateHasChanged();
await Task.Delay(100);
}
});
}
}