1

I'm using NLog to log errors in my WPF application. The logger currently writes to a file. I've now 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

Aks
  • 5,188
  • 12
  • 60
  • 101
  • 1
    did you think to use listview insetad ? Yoiu can bind it to an observable colelction of messages... – Felice Pollano Mar 18 '11 at 14:39
  • You want to display the contents of Nlog log into the RichTextBox. Is this correct? – Marcote Mar 18 '11 at 14:42
  • I'd like to be able to format the error messages. Plus, the user will be able to copy/paste etc. – Aks Mar 18 '11 at 14:43
  • still, Felice's approach is good, it will allow implementing copy/paste/formatting even easier. Though I would propose using `ItemsControl` – Snowbear Mar 18 '11 at 15:07

2 Answers2

0

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

Marcote
  • 2,977
  • 1
  • 25
  • 24
0

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.

Community
  • 1
  • 1
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126