[WPF] 在RichTextBox中输出Microsoft.Extension.Logging库的日志消息 ...

打印 上一主题 下一主题

主题 980|帖子 980|积分 2940

配景

微软的日志库一般是输出到控制台的,但是在WPF中并不能直接利用控制台,需要AllocConsole。
但是这种做法个人觉得不太安全(一关闭控制台整个程序就退出了?)。这时间就需要一个更加友好的方式输出日志。
问题

那如何将日志的内容显示到RichTextBox中?
实现LoggerProcessor


  • 这里参照官方的ConsoleLoggerProcessor,但是需要有点区别。
  1. public class RichTextBoxLoggerProcessor:IDisposable
  2. {
  3.     ///...其他实现请参照Microsoft.Extension.Logging的源码
  4.     private readonly RichTextBoxDocumentStorage _storage;
  5.     private readonly Thread _outputThread;
  6.     /// 这个构造函数传入RichTextBoxDocumentStorage,用于显示单条日志记录
  7.     public RichTextBoxLoggerProcessor(RichTextBoxDocumentStorage storage,       LoggerQueueFullMode fullMode, int maxQueueLength)
  8.     {
  9.         _storage = storage;
  10.         _messageQueue = new();
  11.         FullMode = fullMode;
  12.         MaxQueueLength = maxQueueLength;
  13.         _outputThread = new Thread(ProcessMessageQueue)
  14.         {
  15.             IsBackground = true,
  16.             Name = "RichTextBox logger queue processing thread"
  17.         };
  18.         _outputThread.Start();
  19.     }
  20.     ///改写WriteMessage方法,熟悉FlowDocument的兄弟应该都知道Paragraph是什么吧
  21.     public void WriteMessage(Paragraph message)
  22.     {
  23.         try
  24.         {
  25.             //发送回FlowDocument所在的线程后添加Paragraph
  26.             _storage.Document?.Dispatcher.BeginInvoke(() =>
  27.             {
  28.                 _storage.Document.Blocks.Add(message);
  29.             });
  30.         }
  31.         catch
  32.         {
  33.             CompleteAdding();
  34.         }
  35.     }
  36.     //同理改写EnqueMessage方法和Enqueue等方法
  37.     public void EnqueMessage(Paragraph message)
  38.     {
  39.         //...具体逻辑请参阅github源码
  40.     }
  41.     public bool Enqueue(Paragraph message)
  42.     {
  43.         //...
  44.     }
  45.      public bool TryDequeue(out Paragraph entry)
  46.      {
  47.         //...
  48.      }
  49. }
  50. public class RichTextBoxDocumentStorage
  51. {
  52.     ///因为要使用到DI,所以创建一个类来存放FlowDocument;
  53.     public FlowDocument? Document{ get; set; }
  54. }
复制代码
实现RichTextBoxLogger


  • 这里继承ILogger接口
  1. public class RichTextBoxLogger:ILogger
  2. {
  3.     private string _category;
  4.     private RichTextBoxLoggerProcessor _processor;
  5.     public RichTextBoxLogger(string category, RichTextBoxLoggerProcessor processor, RichTextBoxFormatter formatter)
  6.     {
  7.         _category = category;
  8.         _processor = processor;
  9.         Formatter = formatter;
  10.     }
  11.     //LogEntry格式化器
  12.     public RichTextBoxFormatter Formatter { get; set; }
  13.     public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
  14.     {
  15.         var logEntry = new LogEntry<TState>(logLevel, _category, eventId, state, exception, formatter);
  16.         //paragraph 需要在主线程创建
  17.         App.Current.Dispatcher.BeginInvoke(() =>
  18.         {
  19.             var message = Formatter.Write(in logEntry);
  20.             if (message is null)
  21.             {
  22.               return;
  23.             }
  24.             _processor.EnqueMessage(message);
  25.       });
  26.   }
  27. }
  28. public abstract class RichTextBoxFormatter
  29. {
  30.     protected RichTextBoxFormatter(string name)
  31.     {
  32.         Name = name;
  33.     }
  34.     public string Name { get; }
  35.     public abstract Paragraph? Write<TState>(in LogEntry<TState> logEntry);
  36. }
复制代码
创建LoggerProvider
  1. public class RichTextBoxLoggerProvider: ILoggerProvider
  2. {
  3.     private readonly RichTextBoxFormatter _formatter;
  4.     private readonly ConcurrentDictionary<string,RichTextBoxLogger> _loggers = [];
  5.     private readonly RichTextBoxLoggerProcessor _processor;
  6.     public RichTextBoxLoggerProvider(RichTextBoxDocumentStorage storage, RichTextBoxFormatter formatter)
  7.     {
  8.         _formatter = formatter;
  9.         _processor = new RichTextBoxLoggerProcessor(storage, LoggerQueueFullMode.Wait, 2500);
  10.         _formatter = formatter;
  11.     }
  12.     public ILogger CreateLogger(string categoryName)
  13.     {
  14.         return _loggers.GetOrAdd(categoryName, new RichTextBoxLogger(categoryName, _processor, _formatter));
  15.     }
  16. }
复制代码
创建真正的LogViewer


  • 这里利用的是Window来显现日志
  1. public class LogViewer : Window
  2. {
  3.     public LogViewer(RichTextBoxDocumentStorage storage)
  4.     {
  5.         InitializeComponent();
  6.         if(storage.Document is null)
  7.         {
  8.             //确保FlowDocument是在主线程上创建的
  9.             App.Current.Dispatcher.Invoke(()=>{
  10.                 _storage.Document =  new FlowDocument() { TextAlignment = System.Windows.TextAlignment.Left };
  11.             });
  12.         }
  13.         logPresenter.Document = storage.Document;
  14.     }
  15. }
复制代码
注册服务
  1. public static class RichTextBoxLoggingExtension
  2. {
  3.     public static ILoggingBuilder AddRichTextBoxLogger(this ILoggingBuilder builder)
  4.     {
  5.         builder.Services.AddSingleton<RichTextBoxDocumentStorage>();
  6.         //格式化的实现就不写了,按自己的喜好来写写格式化器;这里是参照的SimpleConsoleFormatter实现的
  7.         builder.Services.AddSingleton<RichTextBoxFormatter, SimpleRichTextBoxFormatter>();
  8.         builder.Services.AddSingleton<ILoggerProvider,RichTextBoxLoggerProvider>();
  9.         return builder;
  10.     }
  11. }
复制代码
详细利用


  • 任意位置利用ServiceProvider唤起LogViewer即可
  1. public class SomeClass
  2. {
  3.     public void OpenLogViewer()
  4.     {
  5.         App.Current.Services.GetRequiredService<LogViewer>().Show();
  6.     }
  7. }
复制代码
结尾

这里只是实现了个简朴的输出,还有很多多少很多多少功能没有实现。
不喜欢写太长的表明说明,感觉好麻烦。代码就是最好的说明(
看哪天心血来潮了,做个nuget包吧。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

吴旭华

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表