In this article I will explain with an example, how to implement error logging and exception handling in ASP.Net using C# and VB.Net.
The errors and exceptions will be written to a Text file as it is easier to find the cause of the error as the Error Log Text File can be easily opened using the basic Notepad application in Windows.
HTML Markup
The following HTML Markup consists of:
Button – For displaying exception (error) message details in Text file.
The Button has been assigned with an OnClick event handler.
<asp:Button Text="Click to Raise Exception" runat="server" OnClick="RaiseException" />
Namespaces
You will need to import the following namespace.
C#
VB.Net
Creating simple Error Log Text File in ASP.Net
When the Button is clicked, an exception is raised by converting a string value to integer inside a Try block.
Then, the raised Exception is caught in the Catch block and the LogError function is called.
LogError
Inside the LogError function, details of the exception (error) along with current date and time are stored in a string variable.
Then, an Error Log Text file is created using Server.MapPath function and StreamWriter class object is created using the path of file.
Finally, using WriteLine method of StreamWriter class object, the details of the exception (error) are written to the Error Log Text file.
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 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;
string path = Server.MapPath("~/ErrorLog/ErrorLog.txt");
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(message);
writer.Close();
}
}
VB.Net
Protected Sub RaiseException(sender As Object, 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(ex As Exception)
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
Dim path As String = Server.MapPath("~/ErrorLog/ErrorLog.txt")
Using writer As New StreamWriter(path, True)
writer.WriteLine(message)
writer.Close()
End Using
End Sub
Screenshot
Downloads