Introduction
Actually when any project is on production and when any error comes than that error is captured in log file. Very easy for programmer as well as client to know about the type of error.
Background
This is an ASP.NET file which is used to log the errors and warning in a file.
The code
Code: CSharp
using System;
using System.IO;
using System.Threading;
using System.Xml;
using System.Text;
using System.Web;
namespace ChangeControl
{
/// <summary>
/// Summary description for Logger.
/// </summary>
///
public enum LOGMODE
{
VERBOSE = 0,
INFO = 1,
WARNING = 2,
ERROR = 3
}
public class Logger
{
static AutoResetEvent logLock = new AutoResetEvent(true);
static string sCommonLogFile;
static string sErrorOnlyLogFile;
//static string sNewAFEsLogFile;
private string sLogFormat;
private string sErrorTime;
public Logger()
{
sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
//this variable used to create log filename format "
//for example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear + sMonth + sDay;
//
// TODO: Add constructor logic here
//
}
public void ErrorLog(string sErrMsg)
{
string sPathName = HttpContext.Current.Server.MapPath("Logs/ErrorLog.txt");
//StreamWriter sw = new StreamWriter(sPathName,true);
StreamWriter sw = new StreamWriter(sPathName + sErrorTime, true);
sw.WriteLine(sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}
public static void Log(LOGMODE mode, string logMessage)
{
try
{
string LogMode = "ERROR";//XmlManager.GetInstance().Read("LogMode");
bool bLogIt = false;
if (LogMode == LOGMODE.ERROR.ToString())
{
bLogIt = (mode == LOGMODE.ERROR) ? true : false;
}
else if (LogMode == LOGMODE.WARNING.ToString())
{
bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING) ? true : false;
}
else if (LogMode == LOGMODE.INFO.ToString())
{
bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING || mode == LOGMODE.INFO) ? true : false;
}
else if (LogMode == LOGMODE.VERBOSE.ToString())
{
bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING || mode == LOGMODE.INFO || mode == LOGMODE.VERBOSE) ? true : false;
}
if (bLogIt)
{
logLock.WaitOne();
sCommonLogFile = @"C:\Inetpub\wwwroot\NewEmployee\NewEmployee\Logs" + DateTime.Now.ToString("ddMMMyyyy").ToUpper() + ".Log";
sErrorOnlyLogFile = @"C:\Inetpub\wwwroot\NewEmployee\NewEmployee\Logs" + "ERROR.Log";
Log(sCommonLogFile, logMessage);
if (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING)
Log(sErrorOnlyLogFile, logMessage);
logLock.Set();
}
}
catch (Exception ex)
{
Logger logger = new Logger();
logger.ErrorLog("Log - " + ex.Message + "::" + ex.StackTrace);
}
}
public static void Log(string sFile, string logMessage)//protected
{
// Initialize Log File for First Time
if (!File.Exists(sFile))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(sFile))
{
sw.WriteLine("+--------------------------------------------+");
sw.WriteLine("| Action Log |");
sw.WriteLine("+--------------------------------------------+");
sw.Flush();
sw.Close();
}
}
// Log all actions in common log file
using (StreamWriter sw = File.AppendText(sFile))
{
sw.Write("{0} : ", DateTime.Now.ToString());
sw.WriteLine(logMessage);
sw.Flush();
sw.Close();
}
}
}
}


