Windows Form应用程序中的基于属性的异常日志记录

时间:2018-08-07 19:41:22

标签: c# .net winforms exception-handling

我已经开始与一个由五个其他开发人员组成的团队一起在现有Windows Forms应用程序上工作。据我了解,该团队还将扩展到新的开发人员。该项目相当大,并且仍要添加新功能。我已经对项目如何处理服务方法中的异常产生了一些担忧。当前,几乎每种服务方法都遵循以下结构:

public void DoSomething( )
{
    try
    {
        // Entire method body
    }
    catch ( Exception e )
    {
        // Five to ten lines of cluttered code
        // for one exception logging call
    }
}

我了解异常处理是自以为是的,甚至可能会引起一些争议。但是,我相信, 每个 服务方法都遵循该模板会带来一系列问题,因为吞没了一些异常而没有解决根本问题。

我一直在阅读best practices for exceptions上的Microsoft文档。我喜欢对不经常发生的事件进行错误处理的想法,并喜欢在事件定期发生时检查代码中的错误情况。

我的问题是,有没有一种方法可以使用基于属性的方法来维护我的异常记录,同时仍然能够处理我的方法中的 some 上的异常。我知道通过扩展System.Web.Mvc.HandleErrorAttribute类或System.Web.Http.Filters.ExceptionFilterAttribute类并重写OnException方法,可以在MVC和Web API中做到这一点。例如,类似:

[ExceptionLogger(className, "Index", "name =dbContext")]
public ActionResult Item(int itemId)
{
    var item = _itemService.GetItem(itemId);
    return View(item);
}

但是,我尚未在WinForms中找到解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

在Windows窗体中没有等效于Dim UniqDate As New Collection ws.Range("I2:I" & lastrow).Select If Not Selection Is Nothing Then For Each cell In Selection UniqDate.Add cell, cell // Collection will ignore the value of "cell" if already exist (duplicated values filtering) // the rest of your code is the same If (cell <> "") And (InStr(tmp, cell) = 0) Then tmp = tmp & cell & "|" End If Next cell End If 的内置函数。

服务层或业务逻辑层

对于服务层或业务逻辑层,您仍然可以使用Web API。您可以在流程中托管Web API,并从操作过滤器中受益。

演示层

对于表示层,您可以依赖以下事件作为异常记录的中心点。

  • HandleError
  • Application.ThreadException