How to Create Log File in C#

During website development, it will be good, if we write log with either error msg / information message. Log file can be simple text file. In this log file we can store the information according our requirement. Writing log not only saves our time as well as helps us to track the application behaviour in proper manner. Please find below the simple C# class which can be called from anywhere by passing the log text.

  
using System;
using System.IO;
using System.Web;

namespace HimasagarNS
{
    /// 
    /// This class used to created log file
    /// 
    public class CreateLogFile
    {
        private string logFormat = string.Empty;
        private string logPath = string.Empty;

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The log message.
        public CreateLogFile(string logMessage)
        {
            LogWrite(logMessage);
        }

        /// 
        /// Logs the write.
        /// 
        /// The log message.
        public void LogWrite(string logMessage)
        {
            logPath = HttpContext.Current.Server.MapPath("~");
            logFormat = DateTime.Now.ToLongDateString().ToString() + " - " + 
                DateTime.Now.ToLongTimeString().ToString() + " ==> ";

            try
            {
                using (StreamWriter writer = File.AppendText(logPath +
                    "\\" + "LogFile.txt"))
                {
                    // Writes a string followed by a line terminator asynchronously to the stream.
                    //writer.WriteLineAsync(logFormat + logMessage ); 

                    writer.WriteLine(logFormat + logMessage);
                }
            }
            catch (Exception ex)
            {
                //Do not do anything 
            }
        }
    }
}

                       


What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More



Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More