借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
1.62 KiB
XCoder
using System;
using System.Globalization;
using Avalonia.Media;
using Avalonia.Media.Immutable;

namespace NewLife.XCoder.Models;

/// <summary>结构化日志条目</summary>
/// <param name="Time">写入时间</param>
/// <param name="Kind">日志类别</param>
/// <param name="Message">日志正文</param>
public sealed record LogEntry(DateTime Time, LogKind Kind, String Message)
{
    private static readonly IBrush _info = new ImmutableSolidColorBrush(Color.Parse("#7C93AC"));
    private static readonly IBrush _tx = new ImmutableSolidColorBrush(Color.Parse("#5FD0FF"));
    private static readonly IBrush _rx = new ImmutableSolidColorBrush(Color.Parse("#6EE7A8"));
    private static readonly IBrush _system = new ImmutableSolidColorBrush(Color.Parse("#7C93AC"));
    private static readonly IBrush _warning = new ImmutableSolidColorBrush(Color.Parse("#FFCF6E"));
    private static readonly IBrush _error = new ImmutableSolidColorBrush(Color.Parse("#FF8A8A"));

    /// <summary>时间文本</summary>
    public String TimeText => Time.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);

    /// <summary>类别标签</summary>
    public String KindLabel => Kind switch
    {
        LogKind.Tx => "TX",
        LogKind.Rx => "RX",
        LogKind.System => "SYS",
        LogKind.Warning => "WARN",
        LogKind.Error => "ERR",
        _ => "INF",
    };

    /// <summary>类别颜色</summary>
    public IBrush KindBrush => Kind switch
    {
        LogKind.Tx => _tx,
        LogKind.Rx => _rx,
        LogKind.System => _system,
        LogKind.Warning => _warning,
        LogKind.Error => _error,
        _ => _info,
    };
}