Files
DE70/.svn/pristine/86/860d19f975a7a33a40e7c564f52c4a77de81c905.svn-base
T
2024-09-02 10:03:23 +07:00

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));
}
}
}