Create log file in asp.net using C# :
                                                                                        Dot net have  various classes to create, write  on a text file . Here I'm using create() method to create a text file and using some classes to reading and writing text files those are TextReader,StreamReader,StreamWriter,TextWriter classes.These are abstract classes .Here i have taken a logfilepath which is the physical path of the text file.If the file is existing in path then we will write (log) messages into this text file.If the file is not existing in the path then we will create a file in this path and write a message into it 
public static string strPath = AppDomain.CurrentDomain.BaseDirectory;
public static string strLogFilePath = strPath + @"log\log.txt";
public static void WriteToLog(string msg)
 {
  try
   {
    if (!File.Exists(strLogFilePath))
       {
       File.Create(strLogFilePath).Close();
       }
    using (StreamWriter w = File.AppendText(strLogFilePath))
       {
        w.WriteLine("\r\nLog: ");
        w.WriteLine("{0}",DateTime.Now.ToString(CultureInfo.InvariantCulture));
        string err = "Error Message:" + msg;
        w.WriteLine(err);
        w.Flush();
        w.Close();
       }
   }

0 comments

Related Posts Plugin for WordPress, Blogger...