How To Use StringBuilder And File Class For .NET Application

In this article, I am going to share how to use StringBuilder and File Class to create an error log for your .NET application.
 
Note
  1. StringBuilder class is available inside System.Text.
  2. File class is available inside System.IO.
Here, I am going to share how and why should we create error log in our application. In our application, generally, we use try, catch block to handle unforeseen errors. As we know, catch block executes when an error occurs in the try block. We write the error inside the catch block. Often, inside the catch block, we call a function which writes the error log. I will explain with the help of an example. Developers use error log to know the type of error and error position in the running application. We write a date, error type, and position of error inside our error log file.
 
Step 1
 
Create one method as I created below. I created one method with WriteErrorLog name and passed two parameters - errorMessage and errorPosition. errorMessage parameter contains an error message and errorPosition parameter contains the method name from where the error is created. I used File class to write the StringBuilder contents over the .txt file inside the bin folder of the application. File class is available inside System.IO namespace.
  1. private void WriteErrorLog(string errorMessage, string errorPostion)   
  2. {  
  3.     StringBuilder sb = new StringBuilder();  
  4.     sb.AppendLine("------------------------Error Log start----------------------------");  
  5.     sb.AppendLine("Date:-" + DateTime.Now.ToString());  
  6.     sb.AppendLine("Error Message:" + errorMessage);  
  7.     sb.AppendLine("Method Name:" + errorPostion);  
  8.     sb.AppendLine("------------------------Error Log End----------------------------");  
  9.     File.WriteAllText(Application.StartupPath + @ "/errorLog.txt", sb.ToString());  
  10. }  
Step 2
 
Call this WriteErrorLog method inside the all catch block and pass these two parameters as I did in the below code. I called WriteErrorLog method inside the catch block. First, the try block will be executed and if any error occurs, then the control goes to catch block and WriteErrorLog method will be called.
 
In my try block, I am trying to convert a string into an integer. At this point, an error will come and control will go to the catch block.  As control goes to the catch block, the WriteErrorLog method will be called and the error log will be created inside the bin folder of the application.
  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.               string mixture = "545f";  
  6.               int number=  int.Parse(mixture);  
  7.             }  
  8.             catch(Exception ex)  
  9.             {  
  10.                 WriteErrorLog(ex.Message, "Button1_Click");  
  11.   
  12.             }              
  13.         }  
Step 3
 
See your bin folder. You will find an errorLog.txt file with some details similar to what I have shown in the below figure.
 
.net
 
With the help of these given details, developers can understand the type of the error and its origin.