Write Log Error File in ASP.NET

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.IO;  
  6. using System.Web.Services.Description;  
  7. using System.Configuration;  
  8. namespace AdCTS_Web.Controller {  
  9.     public class WriteLogFile {  
  10.         public void AddLog(String vMsg) {  
  11.             File.AppendAllText(ErrorLogPath(), Environment.NewLine);  
  12.             File.AppendAllText(ErrorLogPath(), vMsg);  
  13.         }  
  14.         private String ErrorLogPath() {  
  15.             string mErrorLogPath = string.Empty;  
  16.             if (!Directory.Exists(mErrorLogPath)) {  
  17.                 Directory.CreateDirectory(mErrorLogPath);  
  18.             }  
  19.             mErrorLogPath = mErrorLogPath + DateTime.Now.ToString("dd-MMM-yyyy HH") + ".txt";  
  20.             return mErrorLogPath;  
  21.         }  
  22.         public static void Write_LogFile(string msg) {  
  23.             string mLogPath = HttpContext.Current.Server.MapPath(@  
  24.             "\\Logs");  
  25.             string mErrorLogPath = string.Empty;  
  26.             if (!Directory.Exists(mLogPath)) {  
  27.                 {  
  28.                     mLogPath = mLogPath + "\\" + DateTime.Now.ToString("dd-MMM-yyyy-HH");  
  29.                     if (!Directory.Exists(mLogPath)) {  
  30.                         Directory.CreateDirectory(mLogPath);  
  31.                     }  
  32.                     FileStream fs = new FileStream(mLogPath + "\\" + DateTime.Now.ToString("dd_MM_yyyy") + "_error.txt", FileMode.Append, FileAccess.Write);  
  33.                     StreamWriter sw = new StreamWriter(fs);  
  34.                     sw.WriteLine("\n\n\t");  
  35.                     sw.WriteLine(DateTime.Now.ToString("dd_MM_yyyy h:mm tt"));  
  36.                     sw.Write("\t Error :" + msg);  
  37.                     sw.Close();  
  38.                     fs.Close();  
  39.                 }  
  40.             } else {  
  41.                 mLogPath = mLogPath + "\\" + DateTime.Now.ToString("dd-MMM-yyyy-HH");  
  42.                 if (!Directory.Exists(mLogPath)) {  
  43.                     Directory.CreateDirectory(mLogPath);  
  44.                 }  
  45.                 FileStream fs = new FileStream(mLogPath + "\\" + DateTime.Now.ToString("dd_MM_yyyy") + "_error.txt", FileMode.Append, FileAccess.Write);  
  46.                 // FileStream fs = new FileStream("Logs\\" + DateTime.Now.ToString("dd_MM_yyyy") + "_error.txt", FileMode.CreateNew, FileAccess.Write);  
  47.                 StreamWriter sw = new StreamWriter(fs);  
  48.                 sw.WriteLine("\n\n\t");  
  49.                 sw.WriteLine(DateTime.Now.ToString("dd_MM_yyyy h:mm tt"));  
  50.                 sw.Write("\t Error :" + msg);  
  51.                 sw.Close();  
  52.                 fs.Close();  
  53.             }  
  54.         }  
  55.     }  
  56. }