Click here to Skip to main content
15,891,777 members
Articles / Web Development / ASP.NET

Logging Using the Composite Pattern

Rate me:
Please Sign up or sign in to vote.
4.84/5 (36 votes)
15 Jul 2006CPOL3 min read 94.5K   746   95  
Allow flexiable logging using the Composite design pattern.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LoggerExample.Loggers;
using System.Threading;

namespace LoggerExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void m_btnDoSothing_Click(object sender, EventArgs e)
        {
            // create a composite logger with all the logerrs.
            CompositeLogger compositeLogger =   
                new CompositeLogger(new TextBoxLogger(textBox),
                                    new ListBoxLogger(listBox),
                                    new FileLogger("C:\\LogPattern.txt"),
                                    new EventLogger() );


            
            // pass the File Logger
            //DoSomthing(new FileLogger("C://LogMessage.txt")); 
            // txtBox is a text box control on the form
            //DoSomthing(new TextBoxLogger(textBox));

            DoSomthing(compositeLogger);
            
        }

        private void DoSomthing(ILogger logger)
        {
            for(int i=0; i < 10; i++)
            {
                logger.LogMessage("Logging a message " + i.ToString());
             
            }
        }

        private void m_btnDoSomthingThreaded_Click(object sender, EventArgs e)
        {
            // create a composite logger with all the loggers
            CompositeLogger compositeLogger =
                new CompositeLogger(
                        new TextBoxLogger(textBox),
                        new ListBoxLogger(listBox),
                        new FileLogger("C:\\LogPattern.txt"),
                        new EventLogger());

            // create a anonymous function to call DoSomthing
            ParameterizedThreadStart threadDelegate = delegate(object obj)
            {
                ILogger logger = (ILogger)obj;
                DoSomthing(logger);
            };
            
            // Do Somthing in a thread
            Thread t = new Thread(threadDelegate);
            t.Start(compositeLogger);
        }
    }
}

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.

License

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


Written By
Web Developer
Canada Canada
I am currently working as a team leader with a group of amazing .NET programmers. I love coding with .NET, and I love to apply design patterns into my work. Lately I had some free time, so I decided to write some articles, hoping I will spare someone frustration and anxiety.

Comments and Discussions