How to handle the global level exceptions in MVC5.
Introduction
How to handle the global level exceptions in MVC.Background
Handle the exception in MVC5Using the code
Follw the below procedure and code.-> Need to create the one class file common "GlobalExceptionFilter.cs".
-> Register the GlobalExceptionFilter class in Startup.cs file.
Once will get the exception it will hit the OnException mentod globally in that will get all the information about the exception like
- ClassName
- ErrorMessage
- Failurepoint
- Function
- Module
- StackTrace
Blocks of code should be set as style "Formatted" like this:
Hide Shrink
Copy Code
Copy Code public class GlobalExceptionFilter : IExceptionFilter
{
private readonly ILogger _logger = AppLogger.CreateLogger<GlobalExceptionFilter>();
public GlobalExceptionFilter() { }
public void OnException(ExceptionContext context)
{
AlertService alertService = new AlertService();
if (context == null || context.Exception == null)
return;
var exceptionContentForLog = context.Exception.Message;
_logger.LogError(exceptionContentForLog);
_logger.LogError(context.Exception.StackTrace);
AppLogger.LogFileWrite(exceptionContentForLog, context.Exception);
bool isServiceError = true;
try
{
var faultException = (System.ServiceModel.FaultException)context.Exception;
}
catch (Exception ex)
{
isServiceError = false;
}
finally
{
if (isServiceError)
{
context.Result = new HttpStatusCodeResult(403);
}
else
{
SendEmailAfterException(alertService, context.Exception, null);
context.Result = new HttpStatusCodeResult(500);
}
}
}
private void SendEmailAfterException(AlertService alertService, Exception appException, System.ServiceModel.FaultException serviceException)
{
CustomException custException = new CustomException();
var traceInfo = new Byte[200];
var userinfo = string.Empty;
var result = HttpContextHelper.HttpContext.Session.TryGetValue("ExceptionTracingDetails", out traceInfo);
userinfo = ASCIIEncoding.ASCII.GetString(traceInfo);
string[] data = userinfo.Split(',');
if (appException != null)
{
custException.ApplicationName = AppSettings.ApplicationName;
custException.ErrorMessage = appException.Message;
custException.InputParameters = appException.Data.ToString();
if (data[0] != null)
custException.Email = data[0];
if (data[1] != null)
custException.FirstName = data[1];
if (data[2] != null)
custException.LastName = data[2];
custException.StackTrace = appException.StackTrace;
custException.Failurepoint = appException.Source.ToString();
custException.ClassName = appException.TargetSite.ReflectedType.Name;
custException.Function = appException.TargetSite.Name;
custException.Module = appException.TargetSite.Module.ToString();
}
else
{
custException.ApplicationName = AppSettings.ApplicationName;
custException.ClassName = serviceException.TargetSite.ReflectedType.Name;
custException.ErrorMessage = serviceException.Message;
custException.Failurepoint = serviceException.Source.ToString();
custException.Function = serviceException.TargetSite.Name;
custException.InputParameters = serviceException.Data.ToString();
custException.Module = serviceException.TargetSite.Module.ToString();
custException.StackTrace = serviceException.StackTrace;
}
ConcertoHealth.Business.Entities.EmailParams parameter = new Business.Entities.EmailParams();
parameter.CustomException = custException;
alertService.NotifyUser(parameter, Core.Enum.EnumMailType.Exception);
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Filters.Add(new GlobalExceptionFilter());
});
}
No comments:
Post a Comment