将 Global.asax 文件添加到您的 asp.net 项目并处理应用程序错误:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
string errorMessage = "Application Exception: " + ex.Message;
if (ex.InnerException != null)
{
errorMessage += Environment.NewLine + "Inner Exception: " + ex.InnerException.Message;
}
if (Context != null && Context.Request != null)
{
errorMessage += Environment.NewLine + "Absolute Url: " + Context.Request.Url.AbsolutePath;
}
Services.LoggerManager.Log(
Services.LoggerManager.eLogContext.Application,
Services.LoggerManager.eLogType.Error,
Context, errorMessage);
}
这是 LoggerManager 部分:
public static class LoggerManager
{
public enum eLogType
{
Information = 0,
Warning = 1,
Error = 2
}
public enum eLogContext
{
Application = 0,
Session = 1
}
/// <summary>
/// Method for logging custom message
/// </summary>
/// <param name="logContext"></param>
/// <param name="logType"></param>
/// <param name="context"></param>
/// <param name="message"></param>
public static void Log(eLogContext logContext, eLogType logType, HttpContext context, string message)
{
switch (logContext)
{
case eLogContext.Application:
//TODO: log application type event...
break;
case eLogContext.Session:
//TODO: log session type event...
break;
default:
throw new NotImplementedException("eLogContext '" + logContext.ToString() + "' is not implemented!");
}
}
}