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
{
///
/// LogManagement.xaml 的交互逻辑
///
public partial class LogManagement : UserControl
{
///
/// 绑定数据
///
private ObservableCollection bindingData { set; get; } = new ObservableCollection();
///
/// 实时数据
///
private List realTimeDatas { set; get; } = new List();
private BlockingCollection logQueue = new BlockingCollection();
private Dictionary IsHaveKeyDic { set; get; } = new Dictionary();
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)
{
}
///
/// 刷新队列
///
///
///
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), 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;
}
}
public ObservableCollection LogLevelSource
{
get => (ObservableCollection)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 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);
}
}
}
}
}
}