public static class Logging
{
public static void Log(string strLogMsg)
{
System.IO.StreamWriter w = null;
try
{
StackFrame frame = new StackFrame(1, true);// 1 = it will skip the first frame . first frame is the curent frame. true will give the file name
System.Reflection.MethodBase method = frame.GetMethod();
string strLog = method.Name + ":" + method.DeclaringType.FullName + ":" + frame.GetFileLineNumber();
System.IO.FileInfo finfo = new System.IO.FileInfo(Environment.CurrentDirectory + "\\Logfile.log");
w = finfo.AppendText();
w.Write("[" + DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond.ToString() + "]");
w.Write("[" + Environment.UserName + "]");
w.Write("[" + strLog + "]");
w.WriteLine(strLogMsg);
w.Flush();
w.Close();
}
catch(Exception ex)
{
w.Close();
}
}
}
The results of this log file will look like this:
[4/30/2009 4:24:33 PM:980][userName][FucntionName:FileName:LineNumber]
This is very useful to trouble shoot and investigate the root cause of the error.
-Abhinaw
No comments:
Post a Comment