Reputation: 4154
I want to implement some basic tracing in a simple c# (WPF) application that will only be used internally by testers. I basically want the testers to be able to view a log of what they've done and then optionally save it to a file (of their choosing). This would allow the testers to see what they've done thus far in the test and optionally save the log to file for posterity or whatever.
In researching how to implement simple tracing in .Net (using System.Diagnostics.Trace), it seems the default answer is to use log4net (or perhaps NLog, as referenced in Is log4net dead?) instead. My issue with that is that my tracing doesn't need to be fully-featured or anything special, so it seems like overkill to implement an external library (which seems to be backed up by Jeff Atwood himself in a comment in How to save the output of a console application (and others elsewhere, but still...)). If either log4net or NLog is, in fact, the best/only answer (even in my case), I may still accept it (i.e. I'm not entirely closed to the idea of using them, but not terribly excited in it), but try not to harp on (/focus solely on) that one (if possible)...
With that in mind, does anyone have any suggestions on how to implement the listeners such that I can pop open a dialog with the trace log visible (e.g. via a View Log
button) and then optionally save the contents of that dialog (or the trace log itself) to a file (e.g. via a Save Log
button in the `View )?
It seems like it would be relatively simple to implement a listener that simply logs the traces to a collection of some sort in the app, in which case View
simply shows the collection in a listbox or something and Save
just saves that list to file (duh), but then what if the app stays open for a long time and the trace log grows super-large? Perhaps a buffer-type collection that only logs the last x
messages? Suggestions?
Upvotes: 1
Views: 1045
Reputation: 51204
Redirecting your logger's output to a text box will depend on the technology you opted for. I would recommend log4net or NLog (I am using the first one myself) instead of simply using .NET's built in classes (or your own), because it is much more configurable, and it will prove valuable sooner or later.
I am using log4net, and there are several examples on the web of how to attach its output to a text box (like this one, or this one, for example - although this latter example may lack some UI thread synchronization on first sight if used in a multithreaded app). Not only that, but you can also choose to log your data to a database, send it by e-mail, write it to the Windows's Event Log, and many other great things, simply by adding a couple of lines to your config file.
For a first time use, it may seem like an overkill, but it really isn't. Setting up is quick and there are many examples out there. Once you use it, you will continue to add it to all of your future projects.
Upvotes: 1