Error log / time log
//declare global varible of class Stopwatch
Stopwatch stopWatch;
//start the stopwatch
stopWatch.Start();
//place time logger in method
private void LogInvalidLocation(string location, Job item)
{
//code here
TimeLogger(MethodBase.GetCurrentMethod().Name);
}
//timelogger method
private void TimeLogger(string name)
{
stopWatch.Stop();
string logPath = Path.Combine(basePath, "TimeLog");
//Console.WriteLine("Writing in timeLog");
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
string logFile = Path.Combine(logPath, String.Format("TimeLog_{0:yyyy-MM-dd}.txt", DateTime.Now));
//Console.WriteLine(message);
using (StreamWriter writer = new StreamWriter(logFile, true))
{
writer.WriteLine(string.Format("Method name :{0} , Time taken :{1} milliseconds", name, stopWatch.ElapsedMilliseconds));
}
stopWatch = Stopwatch.StartNew();
}
//log in try...catch
try
{
//code here....
}
catch (Exception ex)
{
Log(ex.Message);
}
//log method
private void Log(string message)
{
basePath = @"C:\Users\kaveer\Desktop\XmlFeed\"
logPath = Path.Combine(basePath, "Log");
if (!Directory.Exists(logPath))
Directory.CreateDirectory(logPath);
string logFile = Path.Combine(logPath, String.Format("Log_{0:yyyy-MM-dd}.txt", DateTime.Now));
Console.WriteLine(message);
using (StreamWriter writer = new StreamWriter(logFile, true))
{
writer.WriteLine(string.Format("{0:yyyy.MM.dd HH:mm:ss}: {1}", DateTime.Now, message));
}
}