173 lines
5.6 KiB
C#
173 lines
5.6 KiB
C#
using Cowain.Bake.Common.Core;
|
|
using Prism.Mvvm;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
using System.Windows.Threading;
|
|
using Cowain.Bake.Main.Views;
|
|
using Cowain.Bake.Main.Models;
|
|
using Cowain.Bake.Common;
|
|
|
|
namespace Cowain.Bake.Main.Views
|
|
{
|
|
/// <summary>
|
|
/// LogManagement.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class LogManagement : UserControl
|
|
{
|
|
/// <summary>
|
|
/// 绑定数据
|
|
/// </summary>
|
|
private ObservableCollection<LogModel> bindingData { set; get; } = new ObservableCollection<LogModel>();
|
|
/// <summary>
|
|
/// 实时数据
|
|
/// </summary>
|
|
private List<LogModel> realTimeDatas { set; get; } = new List<LogModel>();
|
|
|
|
private BlockingCollection<LogModel> logQueue = new BlockingCollection<LogModel>();
|
|
private Dictionary<string, MultiComboBox.MultiCbxBaseData> IsHaveKeyDic { set; get; } = new Dictionary<string, MultiComboBox.MultiCbxBaseData>();
|
|
public LogManagement()
|
|
{
|
|
InitializeComponent();
|
|
|
|
if (loglist.ItemsSource == null)
|
|
{
|
|
loglist.ItemsSource = bindingData;
|
|
}
|
|
|
|
Task.Run(() => DispatcherTimer_Tick());
|
|
Unloaded += LogManagement_Unloaded;
|
|
}
|
|
|
|
private void LogManagement_Unloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刷新队列
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
void DispatcherTimer_Tick()
|
|
{
|
|
while (true)
|
|
{
|
|
if (Global.AppExit)
|
|
{
|
|
return;
|
|
}
|
|
|
|
LogModel model = logQueue.Take();
|
|
if (null == model)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var item = realTimeDatas.Find(x => x.LogText == model.LogText);
|
|
if (null != item) //表示有相同的数据
|
|
{
|
|
continue;
|
|
}
|
|
//显示数据
|
|
if (IsHaveKeyDic.ContainsKey(model.LogLevel) && IsHaveKeyDic[model.LogLevel].IsCheck)
|
|
{
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
bindingData.Insert(0, model);
|
|
if (bindingData.Count > Global.MAX_LOG_QTY)
|
|
{
|
|
bindingData.RemoveAt(bindingData.Count - 1);
|
|
}
|
|
});
|
|
|
|
}
|
|
//实时数据
|
|
realTimeDatas.Insert(0, model);
|
|
if (realTimeDatas.Count > Global.MAX_LOG_QTY)
|
|
{
|
|
realTimeDatas.RemoveAt(realTimeDatas.Count - 1);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#region [日志内容入口]
|
|
|
|
private static readonly DependencyProperty LoglistItemProperty = DependencyProperty.Register(
|
|
"LoglistItem", typeof(LogModel), typeof(LogManagement), new PropertyMetadata(null, OnLoglistSourceChange));
|
|
|
|
private static void OnLoglistSourceChange(DependencyObject o, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (o is LogManagement log)
|
|
{
|
|
var model = e.NewValue as LogModel;
|
|
//入队
|
|
log.logQueue.Add(model);
|
|
}
|
|
}
|
|
|
|
public LogModel LoglistItem
|
|
{
|
|
get => (LogModel)GetValue(LoglistItemProperty);
|
|
set => SetValue(LoglistItemProperty, value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region [日志等级源]
|
|
|
|
private static readonly DependencyProperty LogLevelSourceProperty = DependencyProperty.Register(
|
|
"LogLevelSource", typeof(ObservableCollection<MultiComboBox.MultiCbxBaseData>), typeof(LogManagement),
|
|
new PropertyMetadata(null, OnLogLevelSourceChange));
|
|
|
|
private static void OnLogLevelSourceChange(DependencyObject o, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (o is LogManagement log)
|
|
{
|
|
log.mucombo.ItemsSource = e.NewValue as ObservableCollection<MultiComboBox.MultiCbxBaseData>;
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<MultiComboBox.MultiCbxBaseData> LogLevelSource
|
|
{
|
|
get => (ObservableCollection<MultiComboBox.MultiCbxBaseData>)GetValue(LogLevelSourceProperty);
|
|
set => SetValue(LogLevelSourceProperty, value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
private void mucombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (e.Source is MultiComboBox multiComboBox && multiComboBox.ItemsSource is ObservableCollection<MultiComboBox.MultiCbxBaseData> list)
|
|
{
|
|
IsHaveKeyDic = list.ToDictionary(d => d.ViewName, d => d);
|
|
bindingData.Clear();
|
|
foreach (var item in realTimeDatas)
|
|
{
|
|
if (IsHaveKeyDic.ContainsKey(item.LogLevel) && IsHaveKeyDic[item.LogLevel].IsCheck)
|
|
{
|
|
bindingData.Add(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|