28 Aug 2015 @ 9:44 PM 

One of the tricky parts of software development is when you get those defects/bugs that you simply cannot reproduce. These can be because they require particular data in a back-end or because they are intermittent, and sometimes it is because the user is not relating a complete story of what they did to cause the problem. Diagnostics and Debug logging, and only logging stuff that matters, becomes something of an Art form.

I wrote previously about “magically” turning your standard Debug.Print output into a useful debug log in a debug build. (You could also set the DEBUG compile constant for your release build, I suppose). This post follows that up a bit by attempting to “automagically” add logging of User-Interface related events without having to manually add logging to every event handler.

When I first approached this problem- I started with that- “Oh, that’s easy, I’ll just slap a bit of debug output into all the applicable events” I failed to realize the scope of that particular task. hundreds of buttons, some inside user controls, some which needed event handlers added, no less. I decided it would be better to make it a general solution. I decided to go for a WinForms approach, however I’m sure it would be straightforward to adapt to WPF. I toyed with the idea of making it “central”- Such that the same code could be used for Windows Forms or WPF, but I found that approach required a bit more extra code in the actual use, so opted for a more specific solution.

The basic idea is pretty straightforward- have a member variable of desired forms and usercontrols, which, in it’s constructor, takes a ControlCollection from that Form or UserControl and then will hook recognized events for controls. This could benefit from some sort of extensibility as well- so that custom controls could have particular event’s “understood”, but I went with a simple approach. In fact, to start it only understands Buttons and only the Click event.

The class is relatively simple to use. You merely add a member-level variable and initialize it on the Form Load, and hook the UIEvent event to perform the actual logging:

And there you go- now that event logs a bit of useful information about which controls are clicked, which can be used to track some of the User-Interface and how the User is interacting with your application, which can be a valuable tool in attempting to reproduce problems, bugs, and other issues that are being reported by users of your software.

Have something to say about this post? Comment!

Posted By: BC_Programming
Last Edit: 28 Aug 2015 @ 09:44 PM

Categories: .NET, C#


Responses to this post » (None)

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Comment Meta:
RSS Feed for comments

 Last 50 Posts
Change Theme...
  • Users » 45809
  • Posts/Pages » 375
  • Comments » 105
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight


    No Child Pages.

Windows optimization tips

    No Child Pages.

Soft. Picks

    No Child Pages.

VS Fixes

    No Child Pages.