[ACCEPTED]-ASP.Net MVC Exception Logging combined with Error Handling-log4net
I'm still a bit confused with all the different 35 solutions out there, and how attributes 34 can interfere with each other, but I went 33 with this solution:
public class LogErrorsAttribute: FilterAttribute, IExceptionFilter
{
#region IExceptionFilter Members
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.Exception != null)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString();
string loggerName = string.Format("{0}Controller.{1}", controller, action);
log4net.LogManager.GetLogger(loggerName).Error(string.Empty, filterContext.Exception);
}
}
#endregion
}
I still use the [HandleError] attribute 32 as explained in the original question, and 31 I just decorate each controller with a [LogErrors] attribute.
This 30 works for me, as it keeps the error logging 29 in one place and doesn't cause duplicate 28 exceptions to be logged multiple times (which 27 will happen if I extend [HandleError] and 26 use the attribute in multiple places).
I 25 don't think it will be possible to combine 24 both the Exception Logging and Error Handling 23 into one atrribute or class, without it 22 becoming very tedious and complex, or affecting 21 the use of [HandleError]
But this works for 20 me since I decorate each controller only 19 once, with the [LogErrors] attribute, and 18 decorate Controllers and Actions with [HandleError] exactly 17 how I want to, without them interfering 16 with each other.
Update:
Here is an example of How 15 I use it:
[LogErrors(Order = 0)]
[HandleError(Order = 99)]
public class ContactController : Controller
{
public ActionResult Index()
{
return View(Views.Index);
}
public ActionResult Directions()
{
return View(Views.Directions);
}
public ActionResult ContactForm()
{
FormContactMessage formContactMessage = new FormContactMessage();
return View(Views.ContactForm,formContactMessage);
}
[HandleError(ExceptionType = typeof(SmtpException), View = "MessageFailed", Order = 1)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ContactForm(FormContactMessage formContactMessage)
{
if (ModelState.IsValid)
{
if (formContactMessage.IsValid)
{
SmtpClient client = new SmtpClient();
MailAddress recipientAddress = new MailAddress(Properties.Settings.Default.ContactFormRecipientEmailAddress);
MailAddress senderAddress = new MailAddress(Properties.Settings.Default.ContactFormSenderEmailAddress);
MailMessage mailMessage = formContactMessage.ToMailMessage(recipientAddress, senderAddress);
client.Send(mailMessage);
return View("MessageSent");
}
else
{
ModelState.AddRuleViolations(formContactMessage.GetRuleViolations());
}
}
return View(Views.ContactForm, formContactMessage);
}
private static class Views
{
public static string Index { get { return "Index"; } }
public static string Directions { get { return "Directions"; } }
public static string ContactForm { get { return "ContactForm"; } }
}
}
In the above code, SmtpExceptions 14 in the ContactForm
action overload are handled in a 13 very specific way - the user is presented 12 with a ViewPage specific to failed sent 11 messages, in this case it is called "MessageFailed" . All 10 other exceptions are handled by the default 9 behaviour of [HandleError]. Also note that 8 logging of errors occurs first, followed 7 by handling of errors. This is indicated 6 by the following:
[LogErrors(Order = 0)]
[HandleError(Order = 99)]
Update:
There is an alternative 5 solution to this, with a very good explanantion. I 4 recommend reading through it to get a better 3 understanding of the issues involved.
ASP.NET MVC HandleError Attribute, Custom Error Pages and Logging Exceptions (Thanks 2 to Scott Shepherd below, who provided the 1 link in an answer below).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.