增加日志列表

This commit is contained in:
陈国伟 2021-11-02 14:14:57 +08:00
parent 1ef5d2465b
commit a51b2bd807
7 changed files with 160 additions and 1 deletions

View File

@ -87,7 +87,7 @@ namespace FastGithub.UI
var startInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id}",
Arguments = $"ParentProcessId={Process.GetCurrentProcess().Id} UdpLoggerPort={UdpLoggerPort.Value}",
UseShellExecute = false,
CreateNoWindow = true
};

View File

@ -76,10 +76,16 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="UdpLog.cs" />
<Compile Include="UdpLoggerPort.cs" />
<Page Include="FlowChart.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="LogListBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -92,6 +98,9 @@
<Compile Include="FlowChart.xaml.cs">
<DependentUpon>FlowChart.xaml</DependentUpon>
</Compile>
<Compile Include="LogListBox.xaml.cs">
<DependentUpon>LogListBox.xaml</DependentUpon>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>

View File

@ -0,0 +1,32 @@
<UserControl x:Class="FastGithub.UI.LogListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FastGithub.UI"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<ListBox
ItemsSource="{Binding LogList}"
VirtualizingPanel.IsVirtualizing="True"
ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
BorderThickness="0"
Background="#f7f7f7">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0 5" >
<TextBlock TextWrapping="Wrap" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 5" Text="{Binding SourceContext}"/>
<TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Text="{Binding Message}">
<TextBlock.Foreground>
<SolidColorBrush Color="{Binding Color}"/>
</TextBlock.Foreground>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Controls;
namespace FastGithub.UI
{
/// <summary>
/// LogListBox.xaml 的交互逻辑
/// </summary>
public partial class LogListBox : UserControl
{
private readonly byte[] buffer = new byte[ushort.MaxValue];
private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>();
public LogListBox()
{
InitializeComponent();
DataContext = this;
this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
this.BeginReceiveFrom();
}
private void BeginReceiveFrom()
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, null);
}
private void EndReceiveFrom(IAsyncResult ar)
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var length = this.socket.EndReceiveFrom(ar, ref remoteEP);
var json = Encoding.UTF8.GetString(buffer, 0, length);
var log = Newtonsoft.Json.JsonConvert.DeserializeObject<UdpLog>(json);
this.Dispatcher.Invoke(() => this.LogList.Add(log));
this.BeginReceiveFrom();
}
}
}

View File

@ -150,6 +150,12 @@
</Grid>
</TabItem>
<TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Header="日志记录" Height="40" Width="100" Margin="5 0" FontSize="18">
<Grid Background="#f7f7f7">
<local:LogListBox Margin="16"></local:LogListBox>
</Grid>
</TabItem>
<TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Header="问题解答" Height="40" Width="100" Margin="5 0" FontSize="18">
<Grid Background="#f7f7f7">
<WebBrowser Name="webBrowserIssue" />

18
FastGithub.UI/UdpLog.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using System.Windows.Media;
namespace FastGithub.UI
{
public class UdpLog
{
public DateTime Timestamp { get; set; }
public string Level { get; set; }
public string Message { get; set; }
public string SourceContext { get; set; }
public string Color => this.Level == "Information" ? "#333" : "IndianRed";
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace FastGithub.UI
{
/// <summary>
/// udp日志工具类
/// </summary>
static class UdpLoggerPort
{
/// <summary>
/// 获取日志端口
/// </summary>
public static int Value { get; } = GetAvailableUdpPort(38457);
/// <summary>
/// 获取可用的随机Udp端口
/// </summary>
/// <param name="minValue"></param>
/// <param name="addressFamily"></param>
/// <returns></returns>
private static int GetAvailableUdpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork)
{
var hashSet = new HashSet<int>();
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
foreach (var endpoint in tcpListeners)
{
if (endpoint.AddressFamily == addressFamily)
{
hashSet.Add(endpoint.Port);
}
}
for (var port = minValue; port < IPEndPoint.MaxPort; port++)
{
if (hashSet.Contains(port) == false)
{
return port;
}
}
throw new ArgumentException("当前无可用的端口");
}
}
}