良好的错误处理实践 [英] Good error handling practice
问题描述
什么是一个asp.net网站一个很好的错误处理的做法?例子?谢谢!
What is a good error handling practice for an asp.net site? Examples? Thanks!
推荐答案
对于任何.NET项目,我觉得最好的办法是只捕获特定的错误类型,如果他们可以在特定网页上发生。
As with any .net project I find the best way is to only catch specific error types if they are may to happen on the given page.
例如,你可以赶上格式异常的用户给定的输入(只是柜面JavaScript验证失败,你还没有使用的TryParse),但要始终保持顶级水平异常的捕捉到全局错误处理程序。
For example you could catch Format Exceptions for a users given input (just incase JavaScript validation fails and you have not use tryparse) but always leave the catching of the top level Exception to the global error handler.
try
{
//Code that could error here
}
catch (FormatException ex)
{
//Code to tell user of their error
//all other errors will be handled
//by the global error handler
}
您可以使用开源的 ELMAH (错误日志记录模块和处理)用于ASP.Net做这个顶层/全局错误捕获你,如果你想要的。
You can use the open source elmah (Error Logging Modules and Handlers) for ASP.Net to do this top level/global error catching for you if you want.
使用 ELMAH 它可以创建一个错误日志中是可见虽然配置简单的Web界面。您还可以过滤不同类型的错误,并有不同的错误类型自己的自定义错误页。
Using elmah it can create a log of errors that is viewable though a simple to configure web interface. You can also filter different types of errors and have custom error pages of your own for different error types.
这篇关于良好的错误处理实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!