---
title: "How to Create Log File in C#"  
description: "In this blog I have described how to create log file in C#. Log file can be simple text file, XML document or we can also use tables in database to cr"  
author: "AVADHESH PATEL"  
published: 2013-03-06  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/458/how-to-create-log-file-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to Create Log File in C#

In this blog I have described how to create log [file in C#](https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp). [Log file](https://www.mindstick.com/articles/327201/create-log-file-in-c-sharp) can be simple [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file), XML document or we can also use tables in [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) to create log. Here I have used .txt file. In this file we have stored information about generated [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) of application. Using this log file we have easily find out error list. In this file we have stored information according our requirement. For example in log file we have stored [file name](https://www.mindstick.com/interview/34109/how-do-you-generate-a-unique-file-name-to-avoid-overwriting-an-existing-one), exception name, date and time, error line number, event name, Controls name etc.\

##### Now let’s to implementation

Step 1: Write down below line of code in global area within [your application](https://www.mindstick.com/forum/34702/please-tell-me-what-is-cross-site-scripting-and-how-is-it-harmful-for-your-application) such that global class or used it as global.

```
public void LogFile(string sExceptionName, string sEventName, string sControlName, int       nErrorLineNo, string sFormName)        {            StreamWriter log;            if (!File.Exists("logfile.txt"))            {                log = new StreamWriter("logfile.txt");            }            else            {                log = File.AppendText("logfile.txt");            }            // Write to the file:            log.WriteLine("Data Time:" + DateTime.Now);            log.WriteLine("Exception Name:" + sExceptionName);            log.WriteLine("Event Name:" + sEventName);            log.WriteLine("Control Name:" + sControlName);            log.WriteLine("Error Line No.:" + nErrorLineNo);            log.WriteLine("Form Name:" + sFormName);            // Close the stream:            log.Close();        }    }
```

##### Note

· Include System.IO namespace.

· LogFile method call by catch section when any exception is triggered.

· LogFile method takes five [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) that used to write within

logfile.txt.

· logfile.txt is file that stored in your application bin/debug folder.

##### Step 2: Write below ExceptionHelper class (line of code) that return

occurred error line number.

```
public static class ExceptionHelper    {        public static int LineNumber(this Exception e)        {           int linenum = 0;            try            {                linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));            }            catch            {                //Stack trace is not available!            }            return linenum;        }    }
```

##### Step 3: Now put your line of code within try and catch blog and call LogFile

##### method as below

```
try            {                //put here your code            }            catch (Exception exe)            {               //call LogFile method and pass argument as Exception message, event name, control         name, error line number, current form name                LogFile(exe.Message, e.ToString(), ((Control)sender).Name, exe.LineNumber(), this.FindForm().Name);            }
```

Step 4: If any exception is generated then LogFile.txt look like as below

Data Time:3/6/2013 4:29:47 PM

\

Exception Name:Input string was not in a [correct format](https://www.mindstick.com/forum/72/input-string-was-not-in-a-correct-format).Couldn't store

\

<LineNumber> in salary Column. Expected type is Double.

\

Event Name:System.Windows.Forms.MouseEventArgs

\

Control Name:btnUpdate

\

Error Line No.:106

Form Name:Opereation

---

Original Source: https://www.mindstick.com/blog/458/how-to-create-log-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
