일종의 기본 수준 응용 프로그램 오류 처리를 찾는 것처럼 보입니다. Global.asax 파일에서 Application_Error 메서드에 대한 정의를 추가하지 않는 것이 좋습니다. 이것은 (컨트롤러에서, 또는 다른 도서관이나 뷰 등) 응용 프로그램에서 돌발 모든 취소 처리 예외를 잡을 것입니다 여기에
는 예입니다 : 당신의 Global.asax이 추가 :
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
//Do your logging here.
//Redirect to an appropriate error page.
}
하는 경우 무엇을 기록해야하는지에 대해 궁금해 할 때,이 메소드 내부에 접근 할 수있는 예외 객체 내에는 많은 정보가 있습니다. 보통이 정보 중 일부를 텍스트 파일에 쓰는 클래스를 작성합니다.
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);