Code/C#
[C#] Exception Tracing Sample Code
Hide Code
2010. 4. 16. 09:38
string exceptionLogFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), @"Exception.txt");
private void checkBoxTopMost_CheckedChanged(object sender, EventArgs e)
{
try
{
this.TopMost = this.checkBoxTopMost.Checked;
}
catch (Exception ex)
{
StackFrame fr = new StackFrame(true);
StackTrace st = new StackTrace(fr);
MethodBase mb = fr.GetMethod();
recordExceptionLog(ex, mb.Name, fr.GetFileLineNumber(), st.ToString());
}
}
/// <summary>
/// Record Exception Message to Log File
/// </summary>
private void recordExceptionLog(Exception ex, string methodName, int lineNumber, string stackTraceString)
{
string exMessage = String.Format("{0}{1}\t[{2}:{3}] {4}",
DateTime.Now.ToString(), stackTraceString, methodName, lineNumber, ex.Message);
using (StreamWriter sw = new StreamWriter(exceptionLogFile, true))
{
sw.WriteLine(exMessage);
}
}
private void checkBoxTopMost_CheckedChanged(object sender, EventArgs e)
{
try
{
this.TopMost = this.checkBoxTopMost.Checked;
}
catch (Exception ex)
{
StackFrame fr = new StackFrame(true);
StackTrace st = new StackTrace(fr);
MethodBase mb = fr.GetMethod();
recordExceptionLog(ex, mb.Name, fr.GetFileLineNumber(), st.ToString());
}
}
/// <summary>
/// Record Exception Message to Log File
/// </summary>
private void recordExceptionLog(Exception ex, string methodName, int lineNumber, string stackTraceString)
{
string exMessage = String.Format("{0}{1}\t[{2}:{3}] {4}",
DateTime.Now.ToString(), stackTraceString, methodName, lineNumber, ex.Message);
using (StreamWriter sw = new StreamWriter(exceptionLogFile, true))
{
sw.WriteLine(exMessage);
}
}