【发布时间】:2015-11-23 14:11:29
【问题描述】:
我正在寻找一个简单的本机 .NET 错误日志记录解决方案。我对使用 log4net、Elmah 或任何其他日志库不感兴趣。以下是我打算使用的。我的问题是我网站上的性能是最重要的,这个流程是否会引入任何性能问题?
全球.asax
protected void Application_Start(object sender, EventArgs e)
{
GlobalFilters.Filters.Add(new HandleExceptionsAttribute());
}
处理异常属性:
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>
【问题讨论】:
标签: c# asp.net-mvc-5 trace system.diagnostics