I am looking for a simple native .NET error logging solution. I am not interested in using log4net, Elmah or any other logging library. Below is what I am planning on using. My question is performance on my site is paramount and does this flow introduce any performance concerns?
Global.asax
protected void Application_Start(object sender, EventArgs e)
{
GlobalFilters.Filters.Add(new HandleExceptionsAttribute());
}
Handle Exceptions Attribute:
public sealed class HandleExceptionsAttribute : HandleErrorAttribute
{
private void TraceError(Exception _this)
{
Trace.TraceError("{0} Exception {1}", DateTime.Now.ToString("M/d/yyyy h:mmtt"), _this);
}
public override void OnException(ExceptionContext filterContext)
{
var ex = filterContext.Exception;
TraceError(ex);
Task.Run(() =>
{
using (var msg = new MailMessage(ConfigurationManager.AppSettings["SendErrorsFrom"], ConfigurationManager.AppSettings["SendErrorsTo"]))
{
msg.Subject = ex.Message;
msg.Body = ex.StackTrace;
using (var smtp = new SmtpClient())
smtp.Send(msg);
}
});
}
}
web.config:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="textWriterListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="logs\log.txt"/>
<add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="Base Test" />
<remove name="Default"/>
</listeners>
</trace>
</system.diagnostics>