Question Observer Pattern for Logging?

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
About a year ago I threw together a quick and dirty logging system for my class library (it's a dll) using the Observer pattern. Essentially the entire library shares an Observable class through which all messages are sent. Any code using this library can create an observer and subscribe to that class to see any and all messages being sent through the library (errors, warnings, information, and even progress tracking messages). The observer then does whatever it needs to do (save the messages to a file, print the messages to the screen, etc...).

I've never had to use more than a single observer (one for the program using the dll) so I'm wondering if the Observer pattern is really the way to go. I'm about to start work on a new library, and I want to build a good logger into it from the beginning.

So is there a better method for logging than sending everything through a single observable class?
 
You may want to check out Reactive Extensions for .NET (Rx) for its implementations of IObservable(Of T) and IObserver(Of T).

I've posted an example here: http://www.vbdotnetforums.com/mv/44126-mvvm-implementation-discussion.html#post125856 using Reactive Extensions with the Mediator Pattern. The example is showing communication between ViewModels but could easily be adapted to listen for saving, printing, etc.

You may want to check it out so you're not reinventing the wheel.

A couple of good links for RX

(not yet) 101 Rx Samples - Reactive Framework (Rx) Wiki

(not yet) 101 Rx Samples - Reactive Framework (Rx) Wiki

A Brief Introduction to the Reactive Extensions for .NET, Rx - Yet Another Language Geek - Site Home - MSDN Blogs

I would highly recommend the Hands On Lab aka the manual for RX as well.
 
Last edited:
Back
Top