61 lines
1.9 KiB
Plaintext
61 lines
1.9 KiB
Plaintext
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(ObservableCollection<string> logCollection)
|
|
{
|
|
InitializeComponent();
|
|
LogListBox.ItemsSource = logCollection;
|
|
}
|
|
private void LogInfoButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Log.Information("This is an informational log message");
|
|
}
|
|
}
|
|
public class RealTimeLogSink : ILogEventSink
|
|
{
|
|
private readonly IFormatProvider _formatProvider;
|
|
private readonly ObservableCollection<string> _logCollection;
|
|
|
|
public RealTimeLogSink(ObservableCollection<string> logCollection, IFormatProvider formatProvider = null)
|
|
{
|
|
_logCollection = logCollection;
|
|
_formatProvider = formatProvider;
|
|
}
|
|
|
|
public void Emit(LogEvent logEvent)
|
|
{
|
|
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(() => _logCollection.Add(timestampedMessage));
|
|
}
|
|
}
|
|
}
|