To detect the un-handled errors occured in the application we can use Application_Error method in Global.asax file.
Create a table in your Sql Server Database.
CREATE TABLE [dbo].[tblSyatemError]( [ErrorID] [int] IDENTITY(1,1) NOT NULL, [System] [varchar](max) NULL, [DateTime] [varchar](max) NULL, [Message] [varchar](max) NULL, [InnerException] [varchar](max) NULL, [Source] [varchar](max) NULL, [StackTrace] [varchar](max) NULL, CONSTRAINT [PK_tblSyatemError] PRIMARY KEY CLUSTERED ( [ErrorID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]Use below simple code in Global.asax Application_Error method to write the un-handled application level errors to the database.
void Application_Error(object sender, EventArgs args) { // Code that runs when an unhandled error occurs Exception e = Server.GetLastError(); System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("YourConnection"); try { conn.Open(); string query = "INSERT INTO tblSyatemError ([System],[DateTime],[Message],[InnerException],[Source],[StackTrace]) VALUES" + "('" + Request.Url.Host.ToLower().Replace("'", "") + "','" + DateTime.Now + "','" + e.Message.Replace("'", "") + "','" + e.InnerException.ToString().Replace("'", "") + "','" + e.Source.Replace("'", "") + "','" + e.StackTrace.Replace("'", "") + "')"; using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query,conn)) { cmd.ExecuteNonQuery(); } } catch (Exception ex) { } finally { conn.Close(); conn.Dispose(); } }
No comments:
Write comments