How do I create an automatic UI event trace in winforms?

纵然是瞬间 提交于 2020-01-11 12:32:20

问题


I need to take an existing winforms application and drop into an event tracing mode, with hopefully as little friction as possible.

I would like to see every action the user takes as a simple stack trace-looking thing:

MainForm.LaunchThing_Click
ThingWindow.NameInput_Focus
ThingWindow.NameInput_TextChanged
ThingWindow.AddressInput_Focus
ThingWindow.OKButton_Click

and so on.

  • If this was a WPF application, I would do something like filter all events of type WClientInputMessage in ETW.
  • I can't simply dump a stack trace, because that doesn't capture the user's prior actions.
  • I could add logging to every single event [with the recommended practices from this discussion], but this is an extant application with far too many events. Plus, I'm lazy. Gotta be a better way.
  • I can't attach a debugger, I want to get this information automatically attached to bug reports from testing users in the field because [it turns out...] nobody can accurately remember exactly what the sequence of things they clicked on was.

So I'm wondering if anyone has any good tricks, using subclassing or reflection perhaps, to latch on to the UI events.

Performance is not really a concern. And if I can get a hook into every event, that's good enough; I can filter them down to the relevant set fairly easily.


回答1:


How about an observer pattern? Create an EventLogger class with a bunch of overloaded Log methods (one for each event method signature) that do the logging. Instantiate it on application start and subscribe it to each of the UI events.

It should even be possible to do the subscribing automatically with reflection - though I would think it is more effort to set that up than to do it manually for any reasonably sized application. Also you would still have to ensure that your EventLogger class has a Log method for every possible event handler method signature.




回答2:


How many different control types do you have? If you only have a handful of control types that you are looking to trace with, it may be worthwhile to subclass them and do a find and replace on your project to change them to your subclass. This should only take a minute or two.

In your subclassed controls, you could override a small section of the methods, adding tracing before calling the base method.



来源:https://stackoverflow.com/questions/469594/how-do-i-create-an-automatic-ui-event-trace-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!