Hi nauna,
You need to pass ex.Message to email body and url gives you the url where the exception occured. So as per your requirement in mail body you need to pass.
Refer the below sample.
HTML
Default.aspx
<asp:Button ID="Button1" Text="Click to Raise Exception" runat="server" OnClick="RaiseException" />
Default2.aspx
<asp:Button ID="Button1" Text="Click to Raise Exception" runat="server" OnClick="RaiseException2" />
Code
Default.aspx
protected void RaiseException(object sender, EventArgs e)
{
/* If you are using try catch then use throw statement is compulsory
Else if you are not using try catch then it will automatically handel
in Application_Error method in global.asax file.*/
try
{
int i = int.Parse("Default 1");
}
catch (Exception)
{
throw;
}
}
Default2.aspx
protected void RaiseException2(object sender, EventArgs e)
{
/* If you are using try catch then use throw statement is compulsory
Else if you are not using try catch then it will automatically handel
in Application_Error method in global.asax file.*/
try
{
int i = int.Parse("Default 2");
}
catch (Exception)
{
throw;
}
}
Global.asax
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError().GetBaseException();
if (!(ex is HttpException))
{
string url = (HttpContext.Current.Request == null || HttpContext.Current.Request.Url == null) ? "" : HttpContext.Current.Request.Url.AbsoluteUri;
string exceptionMessage = ex.Message;
string StackTrace = ex.StackTrace.Replace(Environment.NewLine, string.Empty);
string source = ex.Source.Replace(Environment.NewLine, string.Empty);
string targetSite = ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty);
/* Call email sending Method from any static class so you can directly use it here .
eg. sendMail.send(); where sendMail is static class having static send email send method.*/
}
}