[WPF] 在RichTextBox中輸出Microsoft.Extension.Logging庫的日志消息
背景
微軟的日志庫一般是輸出到控制臺的,但是在WPF中并不能直接使用控制臺,需要AllocConsole。
但是這種做法個(gè)人覺得不太安全(一關(guān)閉控制臺整個(gè)程序就退出了?)。這時(shí)候就需要一個(gè)更加友好的方式輸出日志。
問題
那如何將日志的內(nèi)容顯示到RichTextBox中?
實(shí)現(xiàn)LoggerProcessor
- 這里參照官方的ConsoleLoggerProcessor,但是需要有點(diǎn)區(qū)別。
public class RichTextBoxLoggerProcessor:IDisposable
{
///...其他實(shí)現(xiàn)請參照Microsoft.Extension.Logging的源碼
private readonly RichTextBoxDocumentStorage _storage;
private readonly Thread _outputThread;
/// 這個(gè)構(gòu)造函數(shù)傳入RichTextBoxDocumentStorage,用于顯示單條日志記錄
public RichTextBoxLoggerProcessor(RichTextBoxDocumentStorage storage, LoggerQueueFullMode fullMode, int maxQueueLength)
{
_storage = storage;
_messageQueue = new();
FullMode = fullMode;
MaxQueueLength = maxQueueLength;
_outputThread = new Thread(ProcessMessageQueue)
{
IsBackground = true,
Name = "RichTextBox logger queue processing thread"
};
_outputThread.Start();
}
///改寫WriteMessage方法,熟悉FlowDocument的兄弟應(yīng)該都知道Paragraph是什么吧
public void WriteMessage(Paragraph message)
{
try
{
//發(fā)送回FlowDocument所在的線程后添加Paragraph
_storage.Document?.Dispatcher.BeginInvoke(() =>
{
_storage.Document.Blocks.Add(message);
});
}
catch
{
CompleteAdding();
}
}
//同理改寫EnqueMessage方法和Enqueue等方法
public void EnqueMessage(Paragraph message)
{
//...具體邏輯請參閱github源碼
}
public bool Enqueue(Paragraph message)
{
//...
}
public bool TryDequeue(out Paragraph entry)
{
//...
}
}
public class RichTextBoxDocumentStorage
{
///因?yàn)橐褂玫紻I,所以創(chuàng)建一個(gè)類來存放FlowDocument;
public FlowDocument? Document{ get; set; }
}
實(shí)現(xiàn)RichTextBoxLogger
- 這里繼承ILogger接口
public class RichTextBoxLogger:ILogger
{
private string _category;
private RichTextBoxLoggerProcessor _processor;
public RichTextBoxLogger(string category, RichTextBoxLoggerProcessor processor, RichTextBoxFormatter formatter)
{
_category = category;
_processor = processor;
Formatter = formatter;
}
//LogEntry格式化器
public RichTextBoxFormatter Formatter { get; set; }
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
var logEntry = new LogEntry<TState>(logLevel, _category, eventId, state, exception, formatter);
//paragraph 需要在主線程創(chuàng)建
App.Current.Dispatcher.BeginInvoke(() =>
{
var message = Formatter.Write(in logEntry);
if (message is null)
{
return;
}
_processor.EnqueMessage(message);
});
}
}
public abstract class RichTextBoxFormatter
{
protected RichTextBoxFormatter(string name)
{
Name = name;
}
public string Name { get; }
public abstract Paragraph? Write<TState>(in LogEntry<TState> logEntry);
}
創(chuàng)建LoggerProvider
public class RichTextBoxLoggerProvider: ILoggerProvider
{
private readonly RichTextBoxFormatter _formatter;
private readonly ConcurrentDictionary<string,RichTextBoxLogger> _loggers = [];
private readonly RichTextBoxLoggerProcessor _processor;
public RichTextBoxLoggerProvider(RichTextBoxDocumentStorage storage, RichTextBoxFormatter formatter)
{
_formatter = formatter;
_processor = new RichTextBoxLoggerProcessor(storage, LoggerQueueFullMode.Wait, 2500);
_formatter = formatter;
}
public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, new RichTextBoxLogger(categoryName, _processor, _formatter));
}
}
創(chuàng)建真正的LogViewer
- 這里使用的是Window來展現(xiàn)日志
public class LogViewer : Window
{
public LogViewer(RichTextBoxDocumentStorage storage)
{
InitializeComponent();
if(storage.Document is null)
{
//確保FlowDocument是在主線程上創(chuàng)建的
App.Current.Dispatcher.Invoke(()=>{
_storage.Document = new FlowDocument() { TextAlignment = System.Windows.TextAlignment.Left };
});
}
logPresenter.Document = storage.Document;
}
}
注冊服務(wù)
public static class RichTextBoxLoggingExtension
{
public static ILoggingBuilder AddRichTextBoxLogger(this ILoggingBuilder builder)
{
builder.Services.AddSingleton<RichTextBoxDocumentStorage>();
//格式化的實(shí)現(xiàn)就不寫了,按自己的喜好來寫寫格式化器;這里是參照的SimpleConsoleFormatter實(shí)現(xiàn)的
builder.Services.AddSingleton<RichTextBoxFormatter, SimpleRichTextBoxFormatter>();
builder.Services.AddSingleton<ILoggerProvider,RichTextBoxLoggerProvider>();
return builder;
}
}
具體使用
- 任意位置使用ServiceProvider喚起LogViewer即可
public class SomeClass
{
public void OpenLogViewer()
{
App.Current.Services.GetRequiredService<LogViewer>().Show();
}
}
結(jié)尾
這里只是實(shí)現(xiàn)了個(gè)簡單的輸出,還有好多好多功能沒有實(shí)現(xiàn)。
不喜歡寫太長的解釋說明,感覺好麻煩。代碼就是最好的說明(
看哪天心血來潮了,做個(gè)nuget包吧。

浙公網(wǎng)安備 33010602011771號