We can handle the Global Level Exceptions by creating the custom exception filter in our application. To create custom exception filter do the following things.
1) Create a Class in the App_Start folder inherit the IExceptionFilter interface.
2) Override the OnException() method.
3) Create the object of the System.Exception Class.
public class LogFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (!(ex is HttpException)) //ignoring the file not found exception
{
//Do your stuff.....!!!
}
}
}
4) Register the new Custom Filter into the Filter.Config.cs
5) Register the filter in Global.asax.cs file.
Note*- For using this we must enable the custom exception in the Web.config file and use the [Handle Error] action filter on either on the controller or action method.
1) Create a Class in the App_Start folder inherit the IExceptionFilter interface.
2) Override the OnException() method.
3) Create the object of the System.Exception Class.
public class LogFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (!(ex is HttpException)) //ignoring the file not found exception
{
//Do your stuff.....!!!
}
}
}
4) Register the new Custom Filter into the Filter.Config.cs
filters.Add(new LogFilter());
5) Register the filter in Global.asax.cs file.
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
Note*- For using this we must enable the custom exception in the Web.config file and use the [Handle Error] action filter on either on the controller or action method.
No comments:
Post a Comment