80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Serilog;
|
|
using Serilog.Core;
|
|
using Serilog.Events;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
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;
|
|
|
|
namespace SafeBagDE70.Admin
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for LogViewerUC.xaml
|
|
/// </summary>
|
|
public partial class LogViewerUC : UserControl
|
|
{
|
|
private ObservableCollection<string> _logItems = new ObservableCollection<string>();
|
|
|
|
public LogViewerUC()
|
|
{
|
|
InitializeComponent();
|
|
LogListView.ItemsSource = AppData.logCollection;
|
|
AppData.logListView = LogListView;
|
|
}
|
|
|
|
private void LogInfoButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Log.Information("This is an informational log message");
|
|
}
|
|
}
|
|
public class RealTimeLogSink : ILogEventSink
|
|
{
|
|
private readonly IFormatProvider _formatProvider;
|
|
|
|
public RealTimeLogSink( IFormatProvider formatProvider = null)
|
|
{
|
|
_formatProvider = formatProvider;
|
|
}
|
|
|
|
public void Emit(LogEvent logEvent)
|
|
{
|
|
try
|
|
{
|
|
var message = logEvent.RenderMessage(_formatProvider);
|
|
var logLevel = logEvent.Level.ToString();
|
|
var timestampedMessage = $"[{logEvent.Timestamp:HH:mm:ss}] [{logLevel}]{message}";
|
|
|
|
// Ensure the message is added on the UI thread
|
|
App.Current.Dispatcher.Invoke(() => {
|
|
AppData.logCollection.Add(timestampedMessage);
|
|
if (AppData.logCollection.Count > AppData.MaxLogCount)
|
|
{
|
|
AppData.logCollection.RemoveAt(0); // Remove the oldest event
|
|
}
|
|
if (AppData.logListView != null && AppData.logListView.Items.Count > 0)
|
|
{
|
|
AppData.logListView.ScrollIntoView(AppData.logListView.Items[AppData.logListView.Items.Count - 1]);
|
|
}
|
|
});
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|