95 lines
2.7 KiB
Text
95 lines
2.7 KiB
Text
@using LANCommander.Server.Logging
|
|
@using Microsoft.AspNetCore.SignalR.Client
|
|
@using XtermBlazor
|
|
@inject ILogger<LogViewer> Logger
|
|
@inject MessageService MessageService
|
|
@inject NavigationManager NavigationManager
|
|
|
|
<Xterm @ref="Terminal" Options="TerminalOptions" Addons="@Addons" />
|
|
|
|
@code {
|
|
Xterm? Terminal;
|
|
HubConnection? HubConnection;
|
|
|
|
TerminalOptions TerminalOptions = new TerminalOptions
|
|
{
|
|
CursorBlink = true,
|
|
CursorStyle = CursorStyle.Bar
|
|
};
|
|
|
|
HashSet<string> Addons = new HashSet<string>()
|
|
{
|
|
"addon-fit"
|
|
};
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
await Connect();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex, "Log viewer failed to connect");
|
|
MessageService.Error("Log viewer failed to connect");
|
|
}
|
|
}
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
await Task.Delay(100);
|
|
|
|
await Terminal.Addon("addon-fit").InvokeVoidAsync("fit");
|
|
}
|
|
}
|
|
|
|
async Task Connect()
|
|
{
|
|
HubConnection = new HubConnectionBuilder()
|
|
.WithUrl(NavigationManager.ToAbsoluteUri("/logging"))
|
|
.Build();
|
|
|
|
HubConnection.On<string, Microsoft.Extensions.Logging.LogLevel, DateTime>("Log", (message, level, timestamp) =>
|
|
{
|
|
var parts = new string[]
|
|
{
|
|
TerminalColor.BrightBlack,
|
|
"[",
|
|
TerminalColor.White,
|
|
timestamp.ToString("HH:mm:ss"),
|
|
TerminalColor.BrightBlack,
|
|
"]",
|
|
GetColorCode(level),
|
|
" ",
|
|
message,
|
|
TerminalColor.Default
|
|
};
|
|
|
|
Terminal?.WriteLine(String.Join("", parts));
|
|
});
|
|
|
|
await HubConnection.StartAsync();
|
|
}
|
|
|
|
string GetColorCode(Microsoft.Extensions.Logging.LogLevel level)
|
|
{
|
|
return level switch
|
|
{
|
|
Microsoft.Extensions.Logging.LogLevel.Trace => TerminalColor.BrightBlack,
|
|
Microsoft.Extensions.Logging.LogLevel.Debug => TerminalColor.BrightCyan,
|
|
Microsoft.Extensions.Logging.LogLevel.Information => TerminalColor.BrightGreen,
|
|
Microsoft.Extensions.Logging.LogLevel.Warning => TerminalColor.BrightYellow,
|
|
Microsoft.Extensions.Logging.LogLevel.Error => TerminalColor.BrightRed,
|
|
Microsoft.Extensions.Logging.LogLevel.Critical => TerminalColor.BrightMagenta,
|
|
_ => TerminalColor.Default,
|
|
};
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (HubConnection is not null)
|
|
await HubConnection.DisposeAsync();
|
|
}
|
|
}
|