In this article I will explain with an example, how to implement Error logging and Exception handling in Windows Forms (WinForms) application using C# and VB.Net.
 
 

Form Design

The following form consists of:
Button – This Button will raise an Exception and the exception (error) Message details will be displayed.
Error logging and Exception handling using C# and VB.Net
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 

Creating simple Error Log Text File in Windows Forms

The following event handler is raised when the Button is clicked. An exception is raised by converting a string value to integer inside a Try-Catch block.
The raised Exception is caught in the Catch block and the LogError function is called.
Inside the LogError function, the details of the exception (error) are written to an Error Log Text file along with current date and time.
C#
private 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/yyyyhh: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 = @"D:\ErrorLog.txt";
     using (StreamWriter writer = new StreamWriter(path, true))
     {
        writer.WriteLine(message);
        writer.Close();
     }
}
 
VB.Net
Private Sub RaiseException(sender As System.Object, e As System.EventArgs) Handles btnRaiseException.Click
    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/yyyyhh: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 "D:\ErrorLog.txt"
     Using writer As New StreamWriter(path, True)
        writer.WriteLine(message)
        writer.Close()
    End Using
End Sub
 
 

Screenshot

Error logging and Exception handling using C# and VB.Net
 
 

Downloads