Thursday, March 19, 2009

How To Create LOG For Our Code Execution Of An Application

To keep track of our code execution we use log files. Here I am giving you a small piece of code for creating log files for out code execution. We will make a project which could have a Logger.cs file which contains a Logger class. The Logger class is a singleton class that could make log for whole execution process of our application. The Logger class contains three methods.

1.       LogDebug(string message):- this method could be used to track the flow of execution of your code. The log will be written into log file with Date and Time of its occurrence.

2.       LogError(Exception ex):- this method could be used to log exceptions that could occur in your code execution. The log will be written into log file with Date and Time of exception occurrence.

3.       LogResult(string message):- this method is to log the result in same file. This will not contain Date and Time of result occurrence.

 

The log file will be opened in Append mode always, whenever we run our application that use logger utility. It means whenever you will run your code using this Logger utility, it will update(append) the log file at specified location if it’s already exists. And if specified log file doesn’t exist at specified location then the log file will be created.

 

You could attach this Looger.cs file in your solutions and could maintain the track of your code execution, exceptions and results.

Please find the code for Logger class below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace SQLServerGACUtilities

{

    ///

    /// Singleton Class for logs.

    ///

    class Logger

    {

        # region Local Variables

        private static Logger logger;

        private static string m_LogFile;

        # endregion

 

        #region Singleton Approach

        private Logger()

        {

        }

        public static Logger CreateInstance(string fileName)

        {

            if (logger == null)

            {

                logger = new Logger();

                m_LogFile = Path.Combine(Directory.GetCurrentDirectory(), fileName);

            }

            return logger;

        }

        #endregion

 

        #region Member Variables

        ///

        /// Log exception

        ///

Exception occured

        public void LogError(Exception ex)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

 

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(String.Concat(DateTime.Now.ToString(), "Error Message: ", ex.Message));

            StreamWr.WriteLine(String.Concat("Error Source:", ex.Source.ToString()));

            StreamWr.WriteLine(String.Concat("Error Details:", ex.ToString()));

            StreamWr.Close();

            logFile.Close();

        }

 

        ///

        /// This function actually logs the information message.

        ///

        ///

message to be logged

        public void LogDebug(string message)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(String.Concat(DateTime.Now.ToString(), " -> ", message));

            StreamWr.Close();

            logFile.Close();

        }

 

        ///

        /// Writes results in Log file.

        ///

        ///

        public void LogResult(string message)

        {

            FileStream logFile = new FileStream(m_LogFile, FileMode.Append, FileAccess.Write, FileShare.Write);

            StreamWriter StreamWr = new StreamWriter(logFile);

            StreamWr.WriteLine(message);

            StreamWr.Close();

            logFile.Close();

        }

        #endregion

    }

 

}

 

 

NOTE:- Please replace the current namespace of the Logger.cs file with your application’s namespace before running the application. Or you could use “using” keyword to register the existing namespace in your application.

 Have a nice day. J

No comments:

Post a Comment