0

我有一个LogOperations类,它是一个单例,它包含我的Log对象的集合和一些用于将 new 添加Log到集合中的方法。Log只有3个属性;Message, Exception, 和Time.

在我的应用程序中的唯一表单上,我得到了对LogOperations. 然后我使用它的Collection<Log> logs属性从每个条目中构建一个数据表并将其放入GridControl(gdcErrorLogs) 中。

        gdcErrorLog.DataSource = logOps.GetLogsAsDataTable(); //Popualtes dt with each Log in logs

我有一堆其他类为表单执行各种功能,每当这些函数捕获错误时,我都会将该错误写入我LogOperations的集合。

每次Log添加一个新条目时,如何使我的表单网格自动更新?我正在考虑在我的LogOperations类中添加对表单的引用,然后在我的添加函数结束时,我只是将表单的网格重新设置为这个新的 DataTable?

public class AddNewLogEventArgs : EventArgs
{
    public Log log { get; private set; } // Error on compile see below
    public AddNewLogEventArgs(Log newLog) // Error on compile see below
    {
        log = newLog;
    }
}

class LogOps
{
    public delegate void AddNewLogHandler(object sender, AddNewLogEventArgs e);
    public event AddNewLogHandler OnAddNewLog;

    private Collection<Log> _logs;
    private static LogOps _singleton;

    private LogOps() 
    {
        _logs = new Collection<Log>();
    }

    public static LogOps Instance
    {
        get 
        {
            if (_singleton == null)
                _singleton = new LogOps();
            return _singleton;
        }
    }

    public void AddLog(string text)
    {
        Log newLog = new Log(text);
        _singleton._logs.Add(newLog);
        OnAddNewLog(new AddNewLogEventArgs(newLog), null);
    }

    public void AddLog(Log log)
    {
        Log newLog = log;
        _singleton._logs.Add(newLog);
        OnAddNewLog(new AddNewLogEventArgs(newLog), null);
    }

    public void AddLog(Exception ex)
    {
        Log newLog = new Log(ex.Message, ex.InnerException);
        _singleton._logs.Add(newLog);
        OnAddNewLog(new AddNewLogEventArgs(newLog), null);
    }

    public DataTable GetLogsAsDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Time");
        dt.Columns.Add("Error");
        dt.Columns.Add("Exception");

        int i = 0;
        foreach (Log log in _logs)
        {
            dt.Rows.Add();

            dt.Rows[i].SetField("Time", log.Time);
            dt.Rows[i].SetField("Error", log.Err);
            dt.Rows[i].SetField("Exception", log.Ex.Message);
            i++;
        }
        return dt;
    }
}

自从尝试 King King 的解决方案以来,编辑代码示例被大量修改

AddNewLogEventArgs 中的两条标记行给了我这两个错误:

  • 错误 1 ​​可访问性不一致:属性类型“PrepareDeployment.Models.Log”的可访问性低于属性“PrepareDeployment.Processes.AddNewLogEventArgs.log”
  • 错误 2 可访问性不一致:参数类型“PrepareDeployment.Models.Log”比方法“PrepareDeployment.Processes.AddNewLogEventArgs.AddNewLogEventArgs(PrepareDeployment.Models.Log)”更难访问
4

1 回答 1

1

你应该这样做:

public delegate void AddNewLogEventHandler(object sender, AddNewLogEventArgs e);   
public event AddNewLogEventHandler AddNewLog;
public class AddNewLogEventArgs : EventArgs {
  public Log {get; private set;}
  public AddNewLogEventArgs(Log log){
    Log = log;
  }
}
protected virtual void OnAddNewLog(AddNewLogEventArgs e){
  AddNewLogEventHandler handler = AddNewLog;
  if(handler != null) handler(this, e);
}
public void AddLog(Exception ex) {
   Log newLog = new Log(ex.Message, ex.InnerException);
   singleton._logs.Add(newLog);       
   OnAddNewLog(new AddNewLogEventArgs(newLog));
}
public void AddLog(string text) {
   Log newLog = new Log(text);
   singleton._logs.Add(newLog);
   OnAddNewLog(new AddNewLogEventArgs(newLog));
}

在您的主表单(使用您的网格)中,您可以执行以下操作:

LogOperations.Instance.AddNewLog += (s,e) => {
   var dt = (DataTable) gdcErrorLog.DataSource;
   dt.Rows.Add(e.Log.Time, e.Log.Err, e.Log.Ex.Message);
};

注意:您应该删除private frmMain frm;旧课程中的 。

于 2013-10-25T17:44:05.977 回答