Files
ThingsGateway/framework/Web/ThingsGateway.Components/Components/ConsoleTxt.razor
T
2023-09-30 23:05:53 +08:00

193 lines
6.5 KiB
Plaintext

@*
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
// QQ群:605534569
//------------------------------------------------------------------------------
*@
@using BlazorComponent;
@using Microsoft.AspNetCore.Components.Web;
@using Microsoft.JSInterop;
@using Masa.Blazor;
@namespace ThingsGateway.Components
<MCardActions>
输出日志
<MSpacer></MSpacer>
@if (ChildContent != null)
@ChildContent
<MTooltip Bottom Context="tip">
<ActivatorContent>
<MButton Class="mx-2" @attributes="@tip.Attrs" Dark Fab Small
OnClick=@(()=>
{
pauseMessage=!pauseMessage;
if(pauseMessage)
{
CurrentMessages= Messages?.ToList();
CurrentMessageStrings= MessageStrings?.ToList();
}
}
)>
<MIcon>@((!pauseMessage) ? "mdi-pause" : "mdi-play")</MIcon>
</MButton>
</ActivatorContent>
<ChildContent>
<span>@((!pauseMessage) ? "暂停日志" : "运行日志")</span>
</ChildContent>
</MTooltip>
<MTooltip Bottom Context="tip">
<ActivatorContent>
<MButton Loading=isDownLoading Class="mx-2" @attributes="@tip.Attrs" Dark Fab Small
OnClick=@(async()=>
{
if(Messages!=null)
await DownDeviceMessageExportAsync(Messages.Select(a=>a.message));
else
await DownDeviceMessageExportAsync(MessageStrings);
}
)>
<MIcon>mdi-export</MIcon>
</MButton>
</ActivatorContent>
<ChildContent>
<span>导出</span>
</ChildContent>
</MTooltip>
<MTooltip Bottom Context="tip">
<ActivatorContent>
<MButton Class="mx-2" @attributes="@tip.Attrs" Dark Fab Small
OnClick=@(()=>
{
Messages?.Clear();
MessageStrings?.Clear();
}
)>
<MIcon>mdi-delete-outline</MIcon>
</MButton>
</ActivatorContent>
<ChildContent>
<span>清空</span>
</ChildContent>
</MTooltip>
</MCardActions>
@{
if (Messages != null)
{
ICollection<(Microsoft.Extensions.Logging.LogLevel level, string message)> item;
if (pauseMessage)
item = CurrentMessages;
else
item = Messages;
<MRow Class="ml-2 mr-2 d-flex" NoGutters>
<MVirtualScroll Context="itemMessage" Height=@(Height) OverscanCount=2 ItemSize="100" Items="item">
<ItemContent>
<div title=@itemMessage.message class=@(itemMessage.level<Microsoft.Extensions.Logging.LogLevel.Information?MasaBlazor.Theme.Dark? " while--text ":"black--text":itemMessage.level>=Microsoft.Extensions.Logging.LogLevel.Warning?" red--text ":"green--text ") style="white-space: nowrap !important;overflow: hidden !important; text-overflow: ellipsis !important;">
@itemMessage.message
</div>
</ItemContent>
</MVirtualScroll>
</MRow>
}
else
{
ICollection<string> item;
if (pauseMessage)
item = CurrentMessageStrings;
else
item = MessageStrings;
<MRow Class="ml-2 mr-2 d-flex" NoGutters>
<MVirtualScroll Context="itemMessage" Height=@(Height) OverscanCount=2 ItemSize="100" Items="item">
<ItemContent>
<div title=@itemMessage style="white-space: nowrap !important;overflow: hidden !important; text-overflow: ellipsis !important;">
@itemMessage
</div>
</ItemContent>
</MVirtualScroll>
</MRow>
}
}
@code {
/// <summary>
/// ChildContent
/// </summary>
[Parameter]
public RenderFragment ChildContent { get; set; }
/// <summary>
/// Height
/// </summary>
[Parameter]
public StringNumber Height { get; set; } = 600;
[Inject]
MasaBlazor MasaBlazor { get; set; }
/// <summary>
/// 日志缓存
/// </summary>
[Parameter]
public ICollection<(Microsoft.Extensions.Logging.LogLevel level, string message)> Messages { get; set; }
/// <summary>
/// 日志缓存
/// </summary>
[Parameter]
public ICollection<string> MessageStrings { get; set; }
private ICollection<(Microsoft.Extensions.Logging.LogLevel level, string message)> CurrentMessages { get; set; }
private ICollection<string> CurrentMessageStrings { get; set; }
/// <summary>
/// 导出
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
private async Task DownDeviceMessageExportAsync(IEnumerable<string> values)
{
try
{
isDownLoading = true;
StateHasChanged();
using var memoryStream = new MemoryStream();
using StreamWriter writer = new(memoryStream);
foreach (var item in values)
{
writer.WriteLine(item);
}
writer.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
using var streamRef = new DotNetStreamReference(stream: memoryStream);
JSObjectReference ??= await JSRuntime.LoadModuleAsync("js/downloadFileFromStream");
await JSObjectReference.InvokeVoidAsync("downloadFileFromStream", $"报文日志导出{DateTimeExtensions.CurrentDateTime.ToString("yyyy-MM-dd HH-mm-ss-fff zz")}.txt", streamRef);
}
finally
{
isDownLoading = false;
}
}
private IJSObjectReference JSObjectReference;
/// <inheritdoc/>
[Inject]
protected IJSRuntime JSRuntime { get; set; }
/// <summary>
/// 导出提示
/// </summary>
private bool isDownLoading;
private bool pauseMessage;
}