title: How to capture all user actions in a WPF application to store them in a log file?
body: <h1>Introduction</h1>
  
  <p>I have a WPF application with code written in C#, developed in Visual Studio 2013. This is a GUI solution and has many interdependent projects, complete with buttons, fields, menus, checkboxes and text.</p>
  
  <h1>Logging user actions</h1>
  
  <p>My goal is to create a log file (a txt file will do) of all actions that an end user takes while using this application. Example actions could be a button-click, writing in a blank text field, (de)select a check-box, close an activity/tab, etc. I want to capture <strong>all</strong> such user interactions in the application UI, and have those actions stored as summarized one line descriptions of the action in a text log file.</p>
  
  <h1>Possible solutions</h1>
  
  <h3>Brute force method</h3>
  
  <p>One way to do this could be to update the event handlers of all the aforementioned elements (button, textfield, ...) with the code to write a summary of the action to append to a txt log file. For example, in an accept button click handler:</p>
  
  <pre class="lang-cs prettyprint-override"><code> private void buttonAccept_Click(object sender, EventArgs e)
   {
       // TODO: default button click handler code...
  
       // TODO: ...and the code to write the event to a txt log file         
   }
  </code></pre>
  
  <p>But this strategy will be very painful to accomplish, having to update each and <em>every</em> event handler (and even write event handlers for elements which don't have any currently).</p>
  
  <h3>Better way(s)</h3>
  
  <p>I am looking for a simpler way in which this could be achieved. Some ideas that I have are:</p>
  
  <p>One way is changing the <strong>event handler base class</strong> (that all other event handlers inherit from?) to contain the log file writing code. This way the log file code will need to be written only once, and it will be called whenever <em>any</em> event handler is called, which is what we want (There needs to be a distinction made between a user generated events and an app generated event, which can prove difficult in case of <em>event chaining</em>. So this could be a challenge).</p>
  
  <p>Another way is using some "<em>sniffer</em>" which works like a debugger and stores the stack trace of an application's methods, as the user moves through it. This would give us only a partial idea of the user's workflows.</p>
  
  <pre><code>    private void buttonAccept_Click(object sender, EventArgs e)
      private bool IsValidName(string userName)
      private void buttonCancel_Click(object sender, EventArgs e)
      private void Close()
      ...
  </code></pre>
  
  <p>Adding a time stamp to the message would be useful...</p>
  
  <pre><code>    3/1/2015 18:19 private void buttonAccept_Click(object sender, EventArgs e)
      3/1/2015 18:19 private bool IsValidName(string userName)
      3/1/2015 18:21 private void buttonCancel_Click(object sender, EventArgs e)
      3/1/2015 18:21 private void Close()
      ...
  </code></pre>
  
  <p>... because, deleting the messages which have a time stamp difference from that of the previous message below a suitable threshold, would leave only the methods that were directly called by the user actions. Assuming that methods signatures are descriptive enough, this message list could work as the desired log file. </p>
  
  <pre><code>    3/1/2015 18:19 private void buttonAccept_Click(object sender, EventArgs e)
      3/1/2015 18:21 private void buttonCancel_Click(object sender, EventArgs e)
      ...
  </code></pre>
  
  <p>However, this will only work for elements that have event handlers associated with them. Still, it's better than the brute force method.</p>
  
  <h1>Suggestion(s)?</h1>
  
  <p>I am having some difficulty in coming up with a way to implement these ideas. Can anyone elaborate on how they could be implemented? Or if you can come up with better ways to make the log file, please share your ideas and also how it can be developed. Thank you!</p>