开发者

WPF: Binding RichTextBox to Logger Output

开发者 https://www.devze.com 2023-02-18 04:25 出处:网络
I\'m using NLog to log errors in my WPF application. The logger currently writes to a file. I\'ve no开发者_开发技巧w created a RichTextBox and want to simultaneously write all session logs to the Rich

I'm using NLog to log errors in my WPF application. The logger currently writes to a file. I've no开发者_开发技巧w created a RichTextBox and want to simultaneously write all session logs to the RichTextBox. I've created a wrapper over the NLog logger so I can include commands to write into some other variable in the wrapper. But how do I do the binding?

Any help is appreciated


I would try something like this:

Create some LogReader class to deal with the read of log error, it should use a StringReader or any reader you like. The LogReader basically will give you a string with the contents of the file. Depending your needs, and specially if the file is too big, maybe you will need to split this into chuncks, but I think you will get the idea. Then you will have a ViewModel Class that basically presents the the data to the RichTextBox

Now comes the tricky part, but with the help of this gem you will be able to do databinding of strings to the RichTextBox.

<RichTextBox attached:RichTextboxAssistant.BoundDocument="{Binding LogMessages}"/>

HTH


I recently found this homemade RichTextBox target for nLog:

http://nlog.codeplex.com/workitem/6272?PendingVoteId=6272

I describe a way to make it work here.

0

精彩评论

暂无评论...
验证码 换一张
取 消