title: Delegate and Events architecture with a Logger
body: <p><strong>Question:</strong>  Is there a simpler way to architect this logging code using events?  </p>
  
  <p>Code inspired from @JonSkeets great talk on www.tekpub.com.  All errors in understanding are my own.</p>
  
  <pre><code>internal class Program
  {
      private static void Main(string[] args)
      {
          Person p = new Person();
          p.GetDefaultName();
      }
  }
  
  public delegate void LogHandler(string message);
  
  public class Person
  {
      public void GetDefaultName()
      {
          LogEventRaiser raiser = SetupLogEventRaiser();
          raiser.OnLog("getting default name");
      }
  
      private static LogEventRaiser SetupLogEventRaiser()
      {
          ConsoleLogger cl = new ConsoleLogger();
          ConsoleLogger2 cl2 = new ConsoleLogger2();
  
          //when delegate LogHander is called, run fileLogger.Logger method
          LogHandler handler = cl.Logger;
          LogHandler handler2 = cl2.Logger;
  
          var raiser = new LogEventRaiser();
          //wire up event called Log which is of type delegate LogHandler
          raiser.Log += handler;
          raiser.Log += handler2;
          return raiser;
      }
  }
  
  public class LogEventRaiser
  {
      public event LogHandler Log;
  
      public void OnLog(string text)
      {
          LogHandler tmp = Log;
          if (tmp != null)
              tmp.Invoke(text);
      }
  }
  
  public class ConsoleLogger
  {
      public void Logger(string s)
      {
          Console.WriteLine("ConsoleLogger: " + s);
      }
  
  }
  
  public class ConsoleLogger2
  {
      public void Logger(string s)
      {
          Console.WriteLine("ConsoleLogger2: " + s);
      }
  }
  </code></pre>