使用到日志接收器的接口 ILogEventSink
WPF+Prsim+Serilog
详细介绍链接 https://github.com/serilog/serilog/wiki/Developing-a-sink
public interface ILogEventSinkWrite:ILogEventSink { LogEvent GetLogMessage(); }
public class LogEventSink : ILogEventSinkWrite { private Channel<LogEvent> _messageChannel; public LogEventSink() { _messageChannel = Channel.CreateUnbounded<LogEvent>(); } public void Emit(LogEvent logEvent) { _messageChannel.Writer.TryWrite(logEvent); } public LogEvent GetLogMessage() { LogEvent logEvent; _messageChannel.Reader.TryRead(out logEvent); return logEvent; } }
public class MainWindowViewModel : BindableBase { private string _title = "Prism Application"; private readonly ILogEventSinkWrite logEventSinkWrite; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } public MainWindowViewModel(ILogEventSinkWrite logEventSinkWrite) { this.logEventSinkWrite = logEventSinkWrite; Log.Logger = new LoggerConfiguration() .WriteTo.Sink(logEventSinkWrite) .CreateLogger(); Log.Information("MainWindowViewModel created"); var res= logEventSinkWrite.GetLogMessage(); this.Title = res.RenderMessage(); ; } }
public partial class App { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.Register<ILogEventSinkWrite, LogEventSink>(); } }
标签:控件,logEventSinkWrite,ILogEventSinkWrite,Serilog,LogEvent,logEvent,UI,messageChan From: https://www.cnblogs.com/-Timosthetic/p/18205791