Click here to Skip to main content
11,790,613 members (58,554 online)
Click here to Skip to main content
Add your own
alternative version

A C# Central Logging Mechanism using the Observer and Singleton Patterns

, 18 Feb 2008 CPOL 72.6K 755 76
A very powerful and clean way to add logging of all sorts to an application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LoggingSample
    public partial class Form1 : Form, ILogger
        #region Data
        // the main Logger object
        private Logger mLogger;
        // a logger observer that will write the log entries to a file
        private FileLogger mFileLogger;
        int mTestCounter;

        #region Form methods
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            // instantiate the logger
            mLogger = Logger.Instance;

            // instantiate the log observer that will write to disk
            mFileLogger = new FileLogger(@"c:\temp\log.txt" );

            // Add mFileLogger and the current form (both of which are Logger observers because
            // they implement the ILogger interface) to the Logger.

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            // The application is shutting down, so ensure the file logger closes the file it's
            // been writing to.

        private void button1_Click(object sender, EventArgs e)
            mTestCounter ++;
            textBox1.Text = mTestCounter.ToString();
            string message = string.Format ("The test counter has just been increased to: {0}", mTestCounter);

        #region ILogger Members

        public void ProcessLogMessage(string message)
            // Form1 implements the ProcessLogMessage method as a simple addition of the incoming
            // message to a textbox on the form.
            textBoxLog.AppendText(message + "\r\n");



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Catriel
Team Leader
Canada Canada
A developer that's been tinkering with computers since he first laid eyes on his buddy's Atari in the mid 80's and messed around with GWBasic and Logo. He now divides his time among his wife, kids, and evil mistress (a term lovingly [ahem...] given to his computer by the wife ...).

For more info, please see my LinkedIn profile:

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1509028.1 | Last Updated 18 Feb 2008
Article Copyright 2007 by David Catriel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid