Monitoring a Windows Forms Application [closed]

三世轮回 提交于 2019-11-28 06:43:35

问题


I would like to monitor user behaviors inside a winforms application.

Is there a general way to hook into the eventing system, without having to write event handlers in every form, or on every button?

I Would like to monitor following events accross the application:

  • Windows opened
  • Windows closed
  • Buttons clicked
  • Ideally: time spend in each form

I dont want to subscribe to all the events in all the forms, the application is to big for that. I would just like to hook in and monitor all events.


回答1:


You don't need to write event handles in every form and for every control. You can put the logic in a base Form class.

You can simply create a base form for your project and put the logic of log there. In the base form you can subscribe for all kinds of events which you need using code.

To apply the solution:

  • You don't need to change designer or designer generated codes. Just derive all forms from BaseForm.
  • It can be simply done using a find and replace all command.

  • Also to create the class below, don't add a Form, just add a class and use the class below:

Base Form

public class BaseForm : Form
{
    public BaseForm()
    {
        if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return;
        this.Load += BaseForm_Load;
        this.FormClosed += BaseForm_FormClosed;
    }
    private IEnumerable<Control> GetAllControls(Control control)
    {
        var controls = control.Controls.Cast<Control>();
        return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls);
    }
    void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        Log(string.Format("{0} Closed", this.Name));
    }
    void BaseForm_Load(object sender, EventArgs e)
    {
        Log(string.Format("{0} Opened", this.Name));
        GetAllControls(this).OfType<Button>().ToList()
            .ForEach(x => x.Click += ButtonClick);
    }
    void ButtonClick(object sender, EventArgs e)
    {
        var button = sender as Button;
        if (button != null) Log(string.Format("{0} Clicked", button.Name));
    }
    public void Log(string text)
    {
        var file = System.IO.Path.Combine(Application.StartupPath, "log.txt");
        text = string.Format("{0} - {1}", DateTime.Now, text);
        System.IO.File.AppendAllLines(file, new string[] { text });
    }
}

Note

  • You can use a log library or any other mechanism for logging.
  • You can use any string format for log.
  • You can simply add property/method to turn on/ turn off log.
  • You may want to log in some other useful events like Application.ThreadException event.
  • You can simply use a StopWatch to calculate the time which user was working with form. Also you can simply log start time and end time and the difference as duration which the user was working with form.
  • Here is the usings which you need for the class:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    


来源:https://stackoverflow.com/questions/39018268/monitoring-a-windows-forms-application

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