Hi mahesh213,
Check with below example.
HTML
<asp:Button Text="Click to Raise Exception" runat="server" OnClick="RaiseException" />
Code
C#
protected void RaiseException(object sender, EventArgs e)
{
try
{
int i = int.Parse("Mudassar");
}
catch (Exception ex)
{
this.LogError(ex);
}
}
private void LogError(Exception ex)
{
string fileName = string.Format("ErrorLog_{0}.txt", DateTime.Now.ToString("MM_dd_yyyy"));
string path = Server.MapPath("~/ErrorLog/") + fileName;
string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
message += string.Format("Message: {0}", ex.Message);
message += Environment.NewLine;
message += string.Format("StackTrace: {0}", ex.StackTrace);
message += Environment.NewLine;
message += string.Format("Source: {0}", ex.Source);
message += Environment.NewLine;
message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path, true))
{
writer.WriteLine(message);
writer.Close();
}
}
VB.Net
Protected Sub RaiseException(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim i As Integer = Integer.Parse("Mudassar")
Catch ex As Exception
Me.LogError(ex)
End Try
End Sub
Private Sub LogError(ByVal ex As Exception)
Dim fileName As String = String.Format("ErrorLog_{0}.txt", DateTime.Now.ToString("MM_dd_yyyy"))
Dim path As String = Server.MapPath("~/ErrorLog/") & fileName
Dim message As String = String.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
message += Environment.NewLine
message += "-----------------------------------------------------------"
message += Environment.NewLine
message += String.Format("Message: {0}", ex.Message)
message += Environment.NewLine
message += String.Format("StackTrace: {0}", ex.StackTrace)
message += Environment.NewLine
message += String.Format("Source: {0}", ex.Source)
message += Environment.NewLine
message += String.Format("TargetSite: {0}", ex.TargetSite.ToString())
message += Environment.NewLine
message += "-----------------------------------------------------------"
message += Environment.NewLine
Using writer As IO.StreamWriter = New IO.StreamWriter(path, True)
writer.WriteLine(message)
writer.Close()
End Using
End Sub