티스토리 뷰

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);
    }
}


'Code > C#' 카테고리의 다른 글

[C#] Windows UI Automation Sample  (0) 2010.04.13
[C#] indexer sample  (0) 2010.02.23
[C#] Shift Operator  (0) 2010.02.17
[C#] Register/Unregister ActiveX Control  (0) 2009.03.15
[C#] Detect UserControl's Closing  (0) 2009.01.07
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함