Managing Exceptions in an ASP.NET MVC Application Using the Enterprise Library

I need information on best practices for managing exceptions. Not how to encode it, there is a lot of information about it, but it’s more like what to do with it in different layers (we have an MVC application in .NET), what to wrap and so on.

Only theoretical information. We want to use the corporate library.

EDIT

Thanks for your answers, but I'm more looking for architectural information. I read a lot of articles on the Internet, like this one in the code project, some on msdn and much more, but basically they solve this problem at the encoding level :(

I'm sure that I will use the corporate library, but I need to understand the script (I hope I said that it was correct) It was very useful

enter image description here

+4
source share
4 answers

I prefer to use error logging in "Application_Error" in global.asax. This allows you to catch any exception that occurs in your web application. I also use try-catch when entering data from untrusted sources (e.g. db access).

I remember how wonderful it was: http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx

A bit about my prod script: We have IIS servers that grow and fall dynamically to compensate for system load and save costs. I am logging errors in a dedicated db, since I cannot get the logs from the server that went down. Collecting logs from multiple servers is also quite inconvenient. I am registering e.ToString () as it saves both the message and the stack trace. Another neat trick: I execute the md5 head on e.ToString () and save it in a separate column in the error table. If this hash already exists, I increment the counter. That is, I group errors so that I can easily find out about the frequency. It also allows you to redirect the user to the error page of your choice after logging. I'm doing it:

Response.Redirect("ErrorPage.html?e=" + errorHash); 

In the html file I will show the hash error from the query string.

+3
source

I wrote some blog posts about exception handling. You can find them here: http://blog.gauffin.org/tag/exceptions/

I basically say:

DO NOT catch this exception!

It is very important to understand that in most cases you should not catch exceptions if you really cannot handle them (or at the top level).

As for ASP.NET MVC, I wrote about the correct way to handle errors here: http://blog.gauffin.org/2011/11/how-to-handle-errors-in-asp-net-mvc/

+2
source

If you want the application error reporting tool to be widely used, I would suggest you look at elmah . This is basically an unobtrusive IIS plugin that provides exception handling for your applications. It also provides many ways to view these exceptions.

If instead you would like to use a common logging system that can be used for both exception reporting and application logging, look at log4net.

Aside, the real question is that you need to register when an exception occurs. To begin with, I wrote down all the information necessary to reproduce the course of events leading to an error.

Each application / company is different from each other, so talk with you team, colleagues and discuss what else you need in addition to this basic rule.

+1
source

It can create its own exception class for each DLL or even for each class. For each custom exception, create your own identifier so that it matches the description that is intended to be tracked and to manage messages displayed to users. An identifier or range of identifiers determines which friendly message to show users. That way, you can identify the exceptions that were caught when they were caught and processed, the exceptions that weren't caught and processed, custom exceptions, as well as track, classify, and find code that throws an exception and much more. To add a record to the mix, you can use log4net or common.logging (works with Entlib). This makes logging easier because it is easy to use and implement, and there is built-in functionality for logging exceptions with all the useful information and it can be used on a large scale.

ADDED: I think I might have had the wrong Base Exception class, and you will notice that DebugFormat and ErrorFormat are our modified log4net code, however I think you get the point.

 namespace Playing.Service { public class UserService { private static readonly ILog log = LogManager.GetLogger(typeof(UserService)); public void SaveUser(string username, string firstName, string lastName) { try { Playing.Repository.UserRepository repository = new Repository.UserRepository(); repository.SaveUser(username, firstName, lastName); log.DebugFormat("Saved User Info"); } catch (Repository.RepositoryException rex) { log.ErrorFormat("Repository Could Not Save User Information: {0}\n Error Message: {1}\nStack Trace: {2}", rex.Message, rex, rex.StackTrace); throw new ServiceException(rex.Message, 2400); } catch (Exception ex) { log.ErrorFormat("Could Not Save User Information: {0}\n Error Message: {1}\nStack Trace: {2}", ex.Message, ex, ex.StackTrace); throw new ServiceException(ex.Message, 12400); } } } public class ServiceException : Playing.Common.BaseException { public ServiceException(string errorMessage) : base(errorMessage) {} public ServiceException(string errorMessage, int errorID) : base(errorMessage, errorID) {} public ServiceException(string errorMessage, params string[] args) : base(errorMessage, args) { } } } namespace Playing.Repository { public class UserRepository { private static readonly ILog log = LogManager.GetLogger(typeof(UserRepository)); public void SaveUser(string username, string firstName, string lastName) { try { //Save data in Database log.DebugFormat("Saved User Info Into Databse"); } catch (Exception ex) { log.ErrorFormat("Could Not Save User Information: {0}\n Error Message: {1}\nStack Trace: {2}", ex.Message, ex, ex.StackTrace); throw new RepositoryException(ex.Message, 3400); } } } public class RepositoryException : Playing.Common.BaseException { public RepositoryException(string errorMessage) : base(errorMessage) {} public RepositoryException(string errorMessage, int errorID) : base(errorMessage, errorID) {} public RepositoryException(string errorMessage, params string[] args) : base(errorMessage, args) {} } } namespace Playing.Common { public class BaseException : Exception { public BaseException(string errorMessage) : base(errorMessage) {} public BaseException(string errorMessage, int errorID) : base(errorMessage) { StringBuilder error = new StringBuilder(); error.Append("("); error.Append(errorID); error.Append("): "); error.Append(errorMessage); errorMessage = error.ToString(); } public BaseException(string errorMessage, params string[] args) : base(errorMessage) { errorMessage = string.Format(errorMessage, args); } } } 
-1
source

Source: https://habr.com/ru/post/1382922/


All Articles