<p>How are you instrumenting your UI's? In the past I've read that people have instrumented their user interfaces, but what I haven't found is examples or tips on <em>how</em> to instrument a UI.</p>

<p>By instrumenting, I mean collecting data regarding usage and performance of the system. A MSDN article on Instrumentation is <a href="http://msdn.microsoft.com/en-us/library/x5952w0c.aspx" rel="nofollow noreferrer" title="here">http://msdn.microsoft.com/en-us/library/x5952w0c.aspx</a>. I would like to capture which buttons users click on, what keyboard shortucts they use, what terms they use to search, etc.</p>

<ul>
<li>How are you instrumenting your UI?</li>
<li>What format are you storing the instrumentation?</li>
<li>How are you processing the instrumented data?</li>
<li>How are you keeping your UI code clean with this instrumentation logic?</li>
</ul>

<p>Specifically, I am implementing my UI in WPF, so this will provide extra challenges compared to instrumenting a web-based application. (i.e. need to transfer the instrumented data back to a central location, etc). That said, I feel the technology may provide an easier implementation of instrumentation via concepts like attached properties.</p>

<ul>
<li>Have you instrumented a WPF application? Do you have any tips on how this can be achieved?</li>
</ul>

<hr />

<p>Edit: The following blog post presents an interesting solution: <a href="https://blog.pixelingene.com/2008/08/techniques-for-ui-auditing-on-wpf-apps/" rel="nofollow noreferrer">Pixel-In-Gene Blog: Techniques for UI Auditing on WPF apps</a></p>

[{'answer_id': 8240, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': -1, 'selected': False, 'text': '<p>I have not yet developed using WPF.. But I would assume that its the same as most other applications in that you want to keep the UI code as light as possible.. A number of design patterns may be used in this such as the obvious <a href="http://en.wikipedia.org/wiki/Model-view-controller" rel="nofollow noreferrer">MVC</a> and <a href="http://en.wikipedia.org/wiki/Facade_pattern" rel="nofollow noreferrer">Façade</a>. I personally always try and keep the objects travelling between the UI and BL layers as light as possible, keeping them to primitives if I can.</p>\n\n<p>This then helps me focus on improving the UI layer without the concerns of anything going on once I throw my (primitive) data back..</p>\n\n<p>I hope I understood your question correctly, and sorry I cannot offer more contextual help with WPF.</p>\n'}, {'answer_id': 8325, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 2, 'selected': False, 'text': '<p>You could consider <a href="http://logging.apache.org/log4net" rel="nofollow noreferrer">log4net</a>. It is a robust logging framework that exists in a single DLL. It is also done in a "non demanding" type mode so that if a critical process is going on, it won\'t log until resources are freed up a bit more.</p>\n\n<p>You could easily setup a bunch of INFO level loggers and track all the user interaction you needed, and it wouldn\'t take a bug crash to send the file to yourself. You could also then log all your ERROR and FATAL code to seperate file that could easily be mailed to you for processing.</p>\n'}, {'answer_id': 8463, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 2, 'selected': False, 'text': '<p>If you make use of WPF commands, each custom command could then log the Action taken.  You can also log the way the command was initiated.</p>\n'}, {'answer_id': 8695, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 0, 'selected': False, 'text': '<p>Perhaps the <a href="http://msdn.microsoft.com/en-us/library/ms747327.aspx" rel="nofollow noreferrer">Microsoft UI Automation</a> for WPF can help out ? Its a framework for automating your UI, perhaps it can be used to log stuff for you...</p>\n\n<p>We use the Automation Framework for auto-testing our UI in WPF.</p>\n'}, {'answer_id': 31514, 'author': 'Brad Leach', 'author_id': 708, 'author_profile': 'https://Stackoverflow.com/users/708', 'pm_score': 3, 'selected': True, 'text': '<p>The following blog post gives quite a few good ideas for instrumenting a WPF application:\n<a href="https://blog.pixelingene.com/2008/08/techniques-for-ui-auditing-on-wpf-apps/" rel="nofollow noreferrer">Techniques for UI Auditing on WPF apps</a>.</p>\n'}, {'answer_id': 164655, 'author': 'Joe Kuemerle', 'author_id': 4273, 'author_profile': 'https://Stackoverflow.com/users/4273', 'pm_score': -1, 'selected': False, 'text': '<p>Disclaimer: I work for the company that sells this product, not only that but I am a developer on this particular product :) .</p>\n\n<p>If you are interested in a commercial product to provide this then Runtime Intelligence (a functional add on to Dotfuscator ) that injects usage tracking functionality into your .NET applications is available.  We provide not only the actual implementation of the tracking functionality but the data collection, processing and reporting functionality as well.</p>\n\n<p>There was recently a discussion on the Business of Software forum on this topic that I also posted in located here: <a href="http://discuss.joelonsoftware.com/default.asp?biz.5.680205.26" rel="nofollow noreferrer">http://discuss.joelonsoftware.com/default.asp?biz.5.680205.26</a> .</p>\n\n<p>For a high level overview of our stuff see here: <a href="http://www.preemptive.com/runtime-intelligence-services.html" rel="nofollow noreferrer">http://www.preemptive.com/runtime-intelligence-services.html</a> .  </p>\n\n<p>In addition I am currently working on writing up some more technically oriented documentation as we realize that is an area we could definitely improve, please let me know if anyone is interested in being notified when I have completed it.</p>\n'}, {'answer_id': 5645456, 'author': 'angularsen', 'author_id': 134761, 'author_profile': 'https://Stackoverflow.com/users/134761', 'pm_score': 3, 'selected': False, 'text': '<p>Here is an example of how I use a simple events manager to hook on to the UI events and extract key information of the events, such as name and type of UI element, name of event and the parent window\'s type name. For lists I also extract the selected item. </p>\n\n<p>This solution only listens for clicks of controls derived from ButtonBase (Button, ToggleButton, ...) and selection changes in controls derived from Selector (ListBox, TabControl, ...). It should be easy to extend to other types of UI elements or to achieve a more fine-grained solution. The solution is inspired of <a href="https://stackoverflow.com/questions/8214/instrumenting-a-ui/31514#31514">Brad Leach\'s answer</a>.</p>\n\n<pre><code>public class UserInteractionEventsManager\n{\n    public delegate void ButtonClickedHandler(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowName);\n    public delegate void SelectorSelectedHandler(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowName, object selectedObject);\n\n    public event ButtonClickedHandler ButtonClicked;\n    public event SelectorSelectedHandler SelectorSelected;\n\n    public UserInteractionEventsManager()\n    {\n        EventManager.RegisterClassHandler(typeof(ButtonBase), ButtonBase.ClickEvent, new RoutedEventHandler(HandleButtonClicked));\n        EventManager.RegisterClassHandler(typeof(Selector), Selector.SelectionChangedEvent, new RoutedEventHandler(HandleSelectorSelected));\n    }\n\n    #region Handling events\n\n    private void HandleSelectorSelected(object sender, RoutedEventArgs e)\n    {\n        // Avoid multiple events due to bubbling. Example: A ListBox inside a TabControl will cause both to send the SelectionChangedEvent.\n        if (sender != e.OriginalSource) return;\n\n        var args = e as SelectionChangedEventArgs;\n        if (args == null || args.AddedItems.Count == 0) return;\n\n        var element = sender as FrameworkElement;\n        if (element == null) return;\n\n        string senderName = GetSenderName(element);\n        string parentWindowName = GetParentWindowTypeName(sender);\n        DateTime time = DateTime.Now;\n        string eventName = e.RoutedEvent.Name;\n        string senderTypeName = sender.GetType().Name;\n        string selectedItemText = args.AddedItems.Count &gt; 0 ? args.AddedItems[0].ToString() : "&lt;no selected items&gt;";\n\n        if (SelectorSelected != null)\n            SelectorSelected(time, eventName, senderName, senderTypeName, parentWindowName, selectedItemText);\n    }\n\n    private void HandleButtonClicked(object sender, RoutedEventArgs e)\n    {\n        var element = sender as FrameworkElement;\n        if (element == null) return;\n\n        string parentWindowName = GetParentWindowTypeName(sender);\n        DateTime time = DateTime.Now;\n        string eventName = e.RoutedEvent.Name;\n        string senderTypeName = sender.GetType().Name;\n        string senderName = GetSenderName(element);\n\n        if (ButtonClicked != null) \n            ButtonClicked(time, eventName, senderName, senderTypeName, parentWindowName);\n    }\n\n    #endregion\n\n    #region Private helpers\n\n    private static string GetSenderName(FrameworkElement element)\n    {\n        return !String.IsNullOrEmpty(element.Name) ? element.Name : "&lt;no item name&gt;";\n    }\n\n\n    private static string GetParentWindowTypeName(object sender)\n    {\n        var parent = FindParent&lt;Window&gt;(sender as DependencyObject);\n        return parent != null ? parent.GetType().Name : "&lt;no parent&gt;";\n    }\n\n    private static T FindParent&lt;T&gt;(DependencyObject item) where T : class\n    {\n        if (item == null) \n            return default(T);\n\n        if (item is T)\n            return item as T;\n\n        DependencyObject parent = VisualTreeHelper.GetParent(item);\n        if (parent == null)\n            return default(T);\n\n        return FindParent&lt;T&gt;(parent);\n    }\n\n    #endregion\n}\n</code></pre>\n\n<p>And to do the actual logging, I use log4net and created a separate logger named \'Interaction\' to log user interaction. The class \'Log\' here is simply my own static wrapper for log4net.</p>\n\n<pre><code>/// &lt;summary&gt;\n/// The user interaction logger uses &lt;see cref="UserInteractionEventsManager"/&gt; to listen for events on GUI elements, such as buttons, list boxes, tab controls etc.\n/// The events are then logged in a readable format using Log.Interaction.Info().\n/// &lt;/summary&gt;\npublic class UserInteractionLogger\n{\n    private readonly UserInteractionEventsManager _events;\n    private bool _started;\n\n    /// &lt;summary&gt;\n    /// Create a user interaction logger. Remember to Start() it.\n    /// &lt;/summary&gt;\n    public UserInteractionLogger()\n    {\n        _events = new UserInteractionEventsManager();\n\n    }\n\n    /// &lt;summary&gt;\n    /// Start logging user interaction events.\n    /// &lt;/summary&gt;\n    public void Start()\n    {\n        if (_started) return;\n\n        _events.ButtonClicked += ButtonClicked;\n        _events.SelectorSelected += SelectorSelected;\n\n        _started = true;\n    }\n\n    /// &lt;summary&gt;\n    /// Stop logging user interaction events.\n    /// &lt;/summary&gt;\n    public void Stop()\n    {\n        if (!_started) return;\n\n        _events.ButtonClicked -= ButtonClicked;\n        _events.SelectorSelected -= SelectorSelected;\n\n        _started = false;\n    }\n\n    private static void SelectorSelected(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowTypeName, object selectedObject)\n    {\n        Log.Interaction.Info("{0}.{1} by {2} in {3}. Selected: {4}", senderTypeName, eventName, senderName, parentWindowTypeName, selectedObject);\n    }\n\n    private static void ButtonClicked(DateTime time, string eventName, string senderName, string senderTypeName, string parentWindowTypeName)\n    {\n        Log.Interaction.Info("{0}.{1} by {2} in {3}", senderTypeName, eventName, senderName, parentWindowTypeName);\n    }\n}\n</code></pre>\n\n<p>The output would then look something like this, omitting non-relevant log entries.</p>\n\n<pre>\n04/13 08:38:37.069 INFO        Iact ToggleButton.Click by AnalysisButton in MyMainWindow\n04/13 08:38:38.493 INFO        Iact ListBox.SelectionChanged by ListView in MyMainWindow. Selected: Andreas Larsen\n04/13 08:38:44.587 INFO        Iact Button.Click by EditEntryButton in MyMainWindow\n04/13 08:38:46.068 INFO        Iact Button.Click by OkButton in EditEntryDialog\n04/13 08:38:47.395 INFO        Iact ToggleButton.Click by ExitButton in MyMainWindow\n</pre>\n'}]
['https://Stackoverflow.com/questions/8214', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/708/']