title: Implementing Audit trails in asp.net Web Forms based on the observer pattern
body: <p>I want to keep track of some user activites in my <code>ASP.NET Web Forms</code> application. For example I'll use the method <code>protected void btnSearch_Click(object sender, EventArgs args)</code> as it is represents more of my business needs and programming problems.</p>
  
  <p>Just to make it more clear. What I want to achieve is, when the <code>btnSearch_Click</code> is executed to make an <code>INSERT</code> to a database table containing some basic information like which exact action was executed, when was executed, who executed it and what was the search word. I'll have a lot of actions that I would like to keep track so the structure of the table is still to be decied but this is the general idea.</p>
  
  <p>My first thought was using some static class like:</p>
  
  <pre><code>public static class UserHistory
  {
    public static void Log(string methodName, string user, string message)
    {
      //execute SQL INSERT
    }
  }
  </code></pre>
  
  <p>and then in the event handlers where I need to keep history track just adding :</p>
  
  <pre><code>UserHistory.Log("btnSearch_Click", "SomeUser", "The user searched for cats");
  </code></pre>
  
  <p>In fact this already seems acceptable to me and it's not that I don't like something in particular in this method but first - this is pretty common tasks, such kind of tasks often already has some patterns helping you to implement the logic in the best possible way and second, I'm not very experienced and I see opportunity here to expand my knowlege by doing something which maybe in this particular case is not that benefitals but good to know anyways.</p>
  
  <p>So this leads me to the <code>Observer</code> pattern. To be honest working with delegates always was troublesome for me, but I'm trying to learn also I know that it's imrpotant to show that you made your own effrots to solve some problem so I'm gonna explain my understanding on I should do this using this pattern but would appretiate some complete answer that I can relate to.</p>
  
  <p>The simplies example that I found was an answer from <code>Jon Skeet</code> where he implemented two classes:</p>
  
  <pre><code> class Observable
      {
          public event EventHandler SomethingHappened;
  
          public void DoSomething()
          {
              EventHandler handler = SomethingHappened;
              if (handler != null)
              {
                  handler(this, EventArgs.Empty);
              }
          }
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>class Observer
  {
      public void HandleEvent(object sender, EventArgs args)
      {
          Console.WriteLine("Something happened to " + sender);
      }
  }
  </code></pre>
  
  <p>and the concrete exmaple to show how this works was:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          Observable observable = new Observable();
          Observer observer = new Observer();
          observable.SomethingHappened += observer.HandleEvent;
  
          observable.DoSomething();
  
      }
  </code></pre>
  
  <p>Maybe this contains enough information to implement this pattern for web forms but I'm unable to detect different roles here in the cotntext of <code>Web Forms</code>.</p>
  
  <p>What I expect is to have a single place where to register all the event that I want to keep track on, something like this:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          observable.SomethingHappened += btnSearch_Click;
          observable.SomethingHappened += btnLogin_Click;
      }
  </code></pre>
  
  <p>but if I try exactly like this, it's literally doing nothing. I don't even go in:</p>
  
  <pre><code>        public void DoSomething()
          {
              EventHandler handler = SomethingHappened;
              if (handler != null)
              {
                  handler(this, EventArgs.Empty);
              }
          }
  </code></pre>
  
  <p>which is logical, but if I have to call this methid explicitly from each method where I want to log something then what is the difference with using a plain old static method instead? Also, I would like to pass a parameter (the string message) which I also can't figure out how to do. So it's obvius that I have poor understanding on how to work with delegates/events but would appreciate if someone helps me with implementing this. For example can be used the <code>btnSearch_Click</code> event handler where I want to pass the search string as parameter.</p>