, - . Application_Error Global.asax. , ( ..).
: Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
}
, , , . , . ( Log) - , , , :
public class Log
{
private StreamWriter _writer;
public void WriteErrorMessage(string errorMessage, string pageUrl, Exception e)
{
_writer = new StreamWriter("LOG_FILE_OUTPUT_PATH_HERE.txt", true);
StringBuilder fullError = new StringBuilder();
fullError.AppendLine("Error log: " + DateTime.Now);
fullError.AppendLine(errorMessage);
fullError.AppendLine("Error raised on: " + pageUrl);
fullError.AppendLine("Associated exception message: " + e.Message + "\n" + e.InnerException);
fullError.AppendLine("Exception class: " + e.GetType().ToString());
fullError.AppendLine("Exception source: " + e.Source.ToString());
fullError.AppendLine("Exception method: " + e.TargetSite.Name.ToString());
fullError.AppendLine();
_writer.WriteLine(fullError);
_writer.Flush();
_writer.Close();
}
}
Application_Error ( ) :
new Log().WriteErrorMessage("Global error has occurred.", Request.Url.ToString(), exception);