I imple开发者_如何学Cmented a simple error logging as a test in a MVC website using the following code:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
using (StreamWriter sw = File.AppendText(Server.MapPath("\\log.txt")))
{
sw.WriteLine(string.Format("Time:{0}\n{1} \n\n\n", DateTime.Now, ex.ToString()));
}
}
The problem i am facing is that logging is not writing to the file if the customerror in the website web.config is not Off
<customErrors mode="Off"></customErrors>
But I think this will let the website users see the yellow screen of death if an error happened, is there a way to log the errors the way I am doing but without setting the custom errors to off?
You will get these Yellow Screen of Dead because you are not handling the exceptions correctly. If you are logging all of the exceptions, then you have (most likely) one method that will receive all of the exceptions. Check this out so that you could better trap errors at Page Level, Application Level or in the Web.Config file.
Take a look at this if you want to see Microsoft's Best Practices for Handling Exceptions.
Good Luck!
精彩评论