Logging to screen

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Logging to screen

skirkby
Hello,

I love what I've seen so far of NLog.

I need to be able to log to the screen (not to the console, but to a Windows form), and would like to know if anyone has any suggestions.

So far, it seems like I should create a new target class and possibly output log events to a RichText box, or some such component.  But I am concerned about thread safety, etc.

Has anyone solved this problem before?

Thanks.

--sk.
Reply | Threaded
Open this post in threaded view
|

Re: Logging to screen

Jarek Kowalski
Administrator
Should be pretty easy. Thread safety can be handled using Control.Invoke or Control.BeginInvoke

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassinvoketopic2.asp

This has some in-depth discussion:

http://weblogs.asp.net/justin_rogers/articles/126345.aspx

I'd love to commit your target to NLog's source base if you decide to publish it.

Regards,

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Logging to screen

skirkby
Thanks, Jarek.

I created a new target class called "ScreenTarget".  I just based it on the MessageBoxTarget class (it's not a descendant of MessageBoxTarget - I just copied the source file and modified it.)

In order to make it work in a thread-safe manner, I needed a delegate that the ScreenTarget.Write() method could call, and I needed a way to marshall the call back to the UI thread.

To do this, I created a new component that descends from the RichTextBox component, and called it ScreenTargetBox.  I also added a field to the ScreenTarget class that takes a reference to a ScreenTargetBox object.

I extended the ScreenTargetBox control by adding a method called "void writeLog( string logMsg )".  I also added a delegate definition, and a class data member that refers to an instance the delegate (created in the constructor) by pointing it to the writeLog() method.

The writeLog() method just calls "this.AppendText(logMsg + "\r\n");".

To make it work, after creating a ScreenTarget object, you have to set its "TargetBox" property to an instance of ScreenTargetBox.  The ScreenTarget object can then call TargetBox.Invoke(TargetBox.logMessage, <text>).

I'm still considering a different approach, but for now, this is working.  (Having a custom control as the "on-screen viewer" would alllow the ability to color-code certain types of items, based on the log text, which can be customized via a layout, etc., but I'm not 100% sure this is the best way to do it...).

Anyway, when I get code more stable I will be glad to contribute it.  I will let you know.

Thanks.

--sk.
Reply | Threaded
Open this post in threaded view
|

Re: Logging to screen

Jarek Kowalski
Administrator
Are you aware of the NLog Viewer?

http://viewer.nlog-project.org/

It's still unreleased, yet but does most of what you want to achieve. The application sends logging events over the network using the UDP protocol and NLogViewer receives them, parses and displays applying coloring, filtering and so on. Most features are not working yet, but as far as viewing and coloring is concerned it's pretty usable.

See this message for more information:

http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200602.mbox/%3C003201c63140$504ed750$2200a8c0@jaakntb%3E

Here's the screenshot:

http://www.ronosaurus.com/log4net/nlogviewer_ndumbster_log4net.png

Hre's the animated demo on NLogViewer being used with iBatis.NET:

http://www.ronosaurus.com/nlog/2005-08-28_NLogViewer_mouse_clicks.htm

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Logging to screen

skirkby
Thanks, Jarek.  The NLog Viewer looks like a great app, and I can think of several applications for it.

Does it use NLog to display log entries received over the network?  If so, I assume then that it uses a Target that displays to a screen window.  Perhaps I could just use that target.

If it doesn't use NLog to display log entries, then it's probably not quite what I am looking for... I need a way for an application's internal logging to not only go to disk, but also to a screen control (one that scrolls in real-time) with the same level of control, filtering, and configurability as other NLog targets.

I had also considered creating a SysLog target so that NLog could log to any SysLog viewer... is this how the NLog Viewer works?  Does it include a target for the logging app?

Thanks.

--sk.