Error Log in global.asax file in asp.net
Write This code appplication_error event in a global.asax
Exception ex = Server.GetLastError() as Exception;
HttpContext.Current.Session[“Error”] = ex.Message;
Server.ClearError();
StringBuilder sb = new StringBuilder();
if (ex != null)
{
sb.Append(“Exception: “);
sb.Append(ex.Message);
if (ex.InnerException != null)
{
sb.Append(” – Inner Exception: “);
sb.Append(ex.InnerException.Message);
}
sb.Append(” – Source: “);
sb.Append(ex.Source);
sb.Append(” – Stack Trace: “);
sb.Append(ex.StackTrace);
Database sqlDatabase = DBConnection.Connect();
string query = “insert into ErrorLog(Error,Msg,Logged_time)values(@stacktrace,@message,@Logged_time)”;
DbCommand Command = sqlDatabase.GetSqlStringCommand(query);
sqlDatabase.AddParameter(Command, “stacktrace”, DbType.String, ParameterDirection.Input, “stacktrace”, DataRowVersion.Current, (ex.InnerException).StackTrace);
sqlDatabase.AddParameter(Command, “message”, DbType.String, ParameterDirection.Input, “message”, DataRowVersion.Current,ex.InnerException.Message);
sqlDatabase.AddParameter(Command, “Logged_time”, DbType.String, ParameterDirection.Input, “message”, DataRowVersion.Current, DateTime.Now);
sqlDatabase.ExecuteNonQuery(Command);
}
