简体   繁体   English

asp.net 中的异常处理

[英]Exception handling in asp.net

Just trying my hands with exception handling block with enterprise library 4.0.只是尝试使用企业库 4.0 处理异常处理块。 My application is an asp.net application that has got a DAL, BLL and UI.我的应用程序是具有 DAL、BLL 和 UI 的 asp.net 应用程序。

When an exception occurs i want to log the relevant scenario/context that would include from DAL(sp, params, exception), BLL (user, role, etc) and UI (session contents).当发生异常时,我想记录相关的场景/上下文,其中包括 DAL(sp、params、异常)、BLL(用户、角色等)和 UI(会话内容)。 Now i suppose i need to create a custom exception with a dictionary.现在我想我需要用字典创建一个自定义异常。 When an exception occurs in DAL, i populate the dictionary with relevant data at every layer as exception bubbles up.当 DAL 中发生异常时,我会在异常冒出时在每一层使用相关数据填充字典。 Finally, i log it at App_Error..最后,我将它记录在 App_Error ..

Examples over web suggest logging it at DAL itself and then propogate. web 上的示例建议将其记录在 DAL 本身,然后进行传播。 What do we do to such a propogated exception at BLL, UI layers?我们如何处理 BLL、UI 层的这种传播异常? Does it mean, it is same as saving the context but every layer itself logs relevant data.这是否意味着,它与保存上下文相同,但每一层本身都会记录相关数据。 And then in log we need to correlate the log statements at every layer to create the context.然后在日志中,我们需要关联每一层的日志语句以创建上下文。

Please suggest...请建议...

In all the solutions i've implemented of this nature common sense normally dictates that you simply report an error from DAL.在我实施的所有具有这种性质的解决方案中,常识通常要求您简单地从 DAL 报告错误。

By transactionally executing any db changes you would ensure a rollback is performed and wrap up the actual exception with your own that states that the inner exception occured and as a result no changes were made.通过以事务方式执行任何数据库更改,您将确保执行回滚并用您自己的异常包裹实际异常,该异常表明发生了内部异常,因此没有进行任何更改。

In the BLL the exception is then caught and then further thrown as a custom "BusinessProcessException" for the calling application.然后在 BLL 中捕获异常,然后将其作为调用应用程序的自定义“BusinessProcessException”进一步抛出。 Optionally the the BLL because it is in its own "box" so to speak sometimes has its own logging mechanism to log business process related exceptions.可选的 BLL 因为它在自己的“盒子”中,可以说有时有自己的日志机制来记录与业务流程相关的异常。

Weather its logged or not at the BLL layer you still need to inform the UI layer (client application) that an exception occurred and was logged passing back the full Exception tree.无论是否在 BLL 层记录它,您仍然需要通知 UI 层(客户端应用程序)发生了异常并已记录并传回完整的异常树。

The client application code in the UI layer may have its own log but that wont be the log the BLL uses. UI 层中的客户端应用程序代码可能有自己的日志,但这不会是 BLL 使用的日志。

The event cycle is something like this...事件周期是这样的......

App calls bll method bll method calls dal method dal method calls stored proc on db stored proc errors throwing (for example) sql exception dal throws new dal exception with sqlexception as inner bll handles dal exception and logs dal fail if there's some transactional element to the bll method that was called, bll rolls back other calls.应用程序调用 bll 方法 bll 方法调用 dal 方法 dal 方法在 db 上调用存储过程 存储过程错误抛出(例如)sql 异常 dal 抛出带有 sqlexception 的新 dal 异常,因为内部 bll 处理 dal 异常并记录 dal 失败,如果有一些事务元素bll 方法被调用,bll 回滚其他调用。 bll throws new bll framework exception inner is dal exception app handles bll exception and decides what to do to proceed with the user experience from the full exception tree. bll 抛出新的 bll 框架异常内部是 dal 异常应用程序处理 bll 异常并决定如何从完整的异常树中继续用户体验。

This is pretty much how all of .net works.这几乎就是所有 .net 的工作原理。

Things to note:注意事项:

If i have a business object that fails an update call, that business object may have several child objects that are updated as part of that call.如果我的业务 object 更新调用失败,则该业务 object 可能有几个子对象作为该调用的一部分进行更新。 Those child objects could be assembled from multiple dal endpoints on different servers and each dal endpoint may have to update several records in potentially several db's这些子对象可以从不同服务器上的多个 dal 端点组装,每个 dal 端点可能必须更新可能几个数据库中的多个记录

So you have a transaction for the bll logic and 1 for each dal calls inner workings, and then potentially 1 within each stored proc call.因此,对于 bll 逻辑,您有一个事务,每个 dal 调用内部工作有 1 个事务,然后在每个存储的 proc 调用中可能有 1 个事务。

What .net does in the background is stack these in such a way that if the transaction at the bll level fails and a rollback is called it should rollback all child transactions, however in the real world where transactions span systems and often networks this may not be possible. .net 在后台所做的是以这样的方式堆栈这些,如果 bll 级别的事务失败并调用回滚,它应该回滚所有子事务,但是在事务跨越系统和通常网络的现实世界中,这可能不是可能的。

You must assume therefore that action you take may fail and can be rollback if any part of the heirarchy fails and the code making the call is responsible for this.因此,您必须假设您采取的操作可能会失败,并且如果层次结构的任何部分失败并且进行调用的代码对此负责,则可以回滚。

what this ultimately means is that you need a full audit trail of what happened.这最终意味着您需要对所发生的事情进行完整的审计跟踪。 as a bll dev i log everything that my framework does anyway, sql also has transaction logs so many dal implementations are simplified in this respect.作为一个 bll 开发人员,我无论如何都会记录我的框架所做的一切,sql 也有事务日志,因此在这方面简化了许多 dal 实现。

Applications have (for asp.net) server logs, or custom options only.应用程序只有(对于 asp.net)服务器日志或自定义选项。

The only thing i would recommend is that each layer logs its own activity and logs are separated (eg a ui related error is not really something that belongs in a bll log)我唯一推荐的是每一层都记录自己的活动并且日志是分开的(例如,与 ui 相关的错误实际上并不属于 bll 日志)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM