Static Events, Logging Sample...

Tags: Tech Geek

I just wanted to share this piece of code with everyone. Essentially I have a Windows Form application with a small little Logger class which exposes a static event, and a static method to "Log" text items. The Windows Form has a Listbox and subscribes to the static event, when the event is triggered it pops the item into the ListBox, very simplistic but handy.

Apologies if this doesnt come out very well, you can also view it here.

Here's your static event logger (Logging.cs):


using System;

namespace StaticEvent {
     public class Logging {
          public delegate void LoggingEvent(object sender,LogEventArgs a);
          public static event LoggingEvent OnLoggingEvent;
          public Logging() {}

          public static void Log(string Text) {
               if(OnLoggingEvent!=null) {
               LogEventArgs a = new LogEventArgs();
               a.Text=Text;
                OnLoggingEvent(null, a);
               }
          }
     }

     public class LogEventArgs {
          public string Text;
     }
}


and now in the win form:


private void Form1_Load(object sender, System.EventArgs e) {
     Logging.OnLoggingEvent+=new StaticEvent.Logging.LoggingEvent(Logging_OnLoggingEvent);
}

private void Logging_OnLoggingEvent(object sender, LogEventArgs a) {
     this.listBox1.Items.Add(a.Text);
}

private void button1_Click(object sender, System.EventArgs e) {
     Logging.Log("hello world");
}


6 Comments

  • Justin Rogers said

    Your sample might have issues in a multi-threaded environment. You need to check this.listBox1.InvokeRequired before simply operating on it to make sure you are in the UI thread.

    Again, in your case, you probably won't run into any issues, but if you started using the static logger from another thread, you might.

  • Prashant said

    Why to use Static key word before event u can use public event delegate Event () i want to know if I use static keyword before evnet then what is a benifit for me

  • Edward said

    for: Prashant, hello, hi there... this is the exact benifit of using a static event... (i.e. 'making logs') you'd need a static event so that it can be called on a STATIC FUNCTION... you need a STATIC FUNCTION to make life easier when logging... (i.e. no need to create an instance of an object just to make a log call) =)

  • omarazam said

    Good stuff. Thanks. Yes, static methods are cool. And static events are cooler. But the coolest is to put it inside a static class. in your case since you really aren't benefiting from creating the logging object, why not? So: public delegate void LoggingEvent(object sender,LogEventArgs a); public static class Logging { public static event LoggingEvent OnLoggingEvent; public static void Log(string Text) { if(OnLoggingEvent!=null) { LogEventArgs a = new LogEventArgs(); a.Text=Text; OnLoggingEvent(null, a); } } }

  • Brent said

    This article is exactly what i was looking for. I'm in a winform app and need a static class to report back the progress from a static method to display. Thanks, Brent

Comments have been disabled for this content.