top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Create Log File in C#?

0 votes
407 views
How to Create Log File in C#?
posted Jul 23, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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 create log. Here I have used .txt file. In this file we have stored information about generated exception 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, 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 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 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.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
answer Jul 23, 2016 by Shivaranjini
...