监视Windows窗体应用程序 [英] Monitoring a Windows Forms Application

查看:121
本文介绍了监视Windows窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视Winforms应用程序内的用户行为.

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已打开
  • Windows关闭
  • 单击的按钮
  • 理想情况:每种形式花费的时间

我不想订阅所有形式的所有事件,因此该应用程序非常重要.我只想挂接并监视所有事件.

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.

推荐答案

您不需要以每种形式和每种控件编写事件句柄.您可以将逻辑放在Form基类中.

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.

要应用解决方案:

  • 您不需要更改设计者或设计者生成的代码.只需从BaseForm导出所有形式.
  • 只需使用查找并替换所有命令即可完成.

  • 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.

也可以在下面创建类,而不添加Form,只需添加一个类并使用下面的类即可:

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

基本表单

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 });
    }
}

注意

  • 您可以使用日志库或任何其他机制进行日志记录.
  • 您可以使用任何字符串格式记录日志.
  • 您只需添加属性/方法即可打开/关闭日志.
  • 您可能要登录其他有用的事件,例如Application.ThreadException事件.
  • 您可以简单地使用StopWatch来计算用户使用表单的时间.您也可以简单地记录开始时间和结束时间,以及将差值记录为用户使用表单的持续时间.
  • 这是您需要在课堂上使用的东西:

  • 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;

这篇关于监视Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆