多层WinForm App中的错误处理

发布于 2024-08-15 18:23:55 字数 309 浏览 2 评论 0原文

例如,如果我有一个带有表示层、业务层和数据层的多层 Winform 应用程序,并且我在业务层或数据层中遇到错误,则唯一的逻辑操作是记录错误并通知用户发生错误应该在哪里记录?

我应该将业务层和数据层中的方法像这样放在 try catch 块中,

try
{
    DoSomethingThatMightGiveErrors();
}
catch(Exception ex)
{
    logger.log(ex.ToString());
    throw;
}

还是应该让错误上升到表示层并处理日志记录并通知用户?

If I have a multi-layer Winform app with a Presentation, Business and Data Layer for example, and I encounter an error in either the Business Layer or Data Layer for which the only logical action is to log the error and inform the user that an error has occurred where should the logging take place?

Should I put the the methods in the Business and Data Layers in try catch blocks like so

try
{
    DoSomethingThatMightGiveErrors();
}
catch(Exception ex)
{
    logger.log(ex.ToString());
    throw;
}

Or should I just let the errors bubble up to the presentation layer and handle the logging and informing the user there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗藏城府 2024-08-22 18:23:55

我将日志记录放在业务层级别,然后重新抛出错误。如果将来在另一个项目中使用该层,它已经在进行日志记录了。重新抛出异常允许该层的使用者将错误转换为友好的消息。

编辑:我认为这有点取决于日志记录的类型:如果您要日志记录到独立于 UI 的中央数据库,请将日志记录放在业务逻辑层中。如果日志记录特定于 UI,例如将日志文件写入应用程序的目录,请将其放入 UI 中。

I'd put the logging at the business layer level and then rethrow the error. If this layer is used in another project in the future, it's already doing the logging. Rethrowing the exception allows consumers of this layer to convert the error into a friendly message.

EDIT: I think it depends a bit on the kind of logging: if you're logging to a central database, which is independent from the UI, put the logging in the business logic layer. If the logging is specific to the UI, for example writing a log file to the application's directory, put it in the UI.

离旧人 2024-08-22 18:23:55

如果您正在谈论未处理的(与业务无关的)异常,只需让它们传播到 UI 层,您可以在其中捕获/记录/通知用户。

If you are talking about unhandled (non-business related) exceptions just let them propagate to the UI layer where you could catch/log/inform the user.

未央 2024-08-22 18:23:55

我的偏好是将其放置在业务层中。

  1. 如果你改变了本质
    表示层(即
    winforms 到 webforms)日志记录
    代码不需要重复。
  2. 您的所有日志记录将更容易查找和维护,因为您始终可以扫描业务类的方法列表并检查/调整它们的日志记录。如果将日志记录放在表示层中,则日志记录调用将分散在各处 - 单个业务类可以在许多不同的表示类中进行日志记录调用。

My preference would be to place it in the business layer.

  1. If you ever change the nature of
    the presentation layer (i.e.
    winforms to webforms) the logging
    code won't need to be duplicated.
  2. All of your logging will be a lot easier to find and maintain, as you can always scan the business class's list of methods and inspect/tweak them for logging. If you put the logging in your presentation layer, the logging calls will be scattered all over the place - a single business class could have logging calls made in many different presentation classes.
千纸鹤 2024-08-22 18:23:55

这取决于您的要求,例如,您是否希望由于冒泡到表示层而出现错误而使用户远离您的应用程序?这些错误在意外情况下发生的频率是多少?

这是一个负载问题,每个应用程序都是不同的,我能说的最基本的事情是在业务/数据层中使用 try catch 子句,并确保通知用户可能会出现错误的某些情况(您这样做文档中有这个吗?)

除此之外,请检查最终用户的要求和反馈...如果您允许错误出现在表示层上,最坏的情况是用户将拒绝使用它,因为错误喷涌而出...

希望这对您有帮助,
此致,
汤姆.

That depends on your requirements, like, for instance, do you want to put your users off your application as a result of errors showing up via bubbling up to the presentation layer? How often would these errors occur in unexpected situations?

This is a loaded question and every application is different, the most basic thing I can say is to use the try catch clauses in the business/data layers and ensure that you inform the users of certain situations where an error could be expected (You do have this in documentation?)

Other than that, check with the requirements and feedback from end users...if you allow the errors to appear on the presentation layer, the worst case is the user will refuse to work with it as a result of errors spewing out...

Hope this helps you,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文