WPF & C# - Runtime.CompilerServices / TraceMessage 로그 파일 만들기 ( Log ) |
MainWindow.xaml.cs
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | int traceNum = 0; // Trace 체크번호 static public void TraceMessage(int traceNum = 0, // Trace 체크번호 string message = "", // 남길 메세지 [CallerMemberName] string memberName = "", // 호출 함수의 이름 [CallerFilePath] string sourceFilePath = "", // 호출 함수가 포함된 파일의 전체 경로 [CallerLineNumber] int sourceLineNumber = 0 // 함수를 호출한 라인 넘버 ) { LogWrite("--- [traceNum] ---- " + traceNum); LogWrite("message: " + message); LogWrite("member name: " + memberName); LogWrite("source file path: " + sourceFilePath); LogWrite("source line number: " + sourceLineNumber); } private static void LogWrite(string str) { string DirPath = Environment.CurrentDirectory + @"\Log"; string FilePath = DirPath + "\\Log_" + DateTime.Today.ToString("yyyyMMdd") + ".log"; string temp; DirectoryInfo di = new DirectoryInfo(DirPath); FileInfo fi = new FileInfo(FilePath); try { if (!di.Exists) Directory.CreateDirectory(DirPath); if (!fi.Exists) { using (StreamWriter sw = new StreamWriter(FilePath)) { temp = string.Format("[{0}] {1}", DateTime.Now, str); sw.WriteLine(temp); sw.Close(); } } else { using (StreamWriter sw = File.AppendText(FilePath)) { temp = string.Format("[{0}] {1}", DateTime.Now, str); sw.WriteLine(temp); sw.Close(); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } } | cs |
사용방법
출력 결과
[2018-06-11 오후 12:36:39] --- [traceNum] ---- 1
[2018-06-11 오후 12:36:39] message:
[2018-06-11 오후 12:36:39] member name: btn_Click
[2018-06-11 오후 12:36:39] source file path: c:\users\admin\source\repos\WpfApp25\WpfApp25\MainWindow.xaml.cs
[2018-06-11 오후 12:36:39] source line number: 33
[2018-06-11 오후 12:36:39] --- [traceNum] ---- 2
[2018-06-11 오후 12:36:39] message:
[2018-06-11 오후 12:36:39] member name: btn_Click
[2018-06-11 오후 12:36:39] source file path: c:\users\admin\source\repos\WpfApp25\WpfApp25\MainWindow.xaml.cs
[2018-06-11 오후 12:36:39] source line number: 33
[2018-06-11 오후 12:36:39] --- [traceNum] ---- 3
[2018-06-11 오후 12:36:39] message:
[2018-06-11 오후 12:36:39] member name: btn_Click
[2018-06-11 오후 12:36:39] source file path: c:\users\admin\source\repos\WpfApp25\WpfApp25\MainWindow.xaml.cs
[2018-06-11 오후 12:36:39] source line number: 33
[2018-06-11 오후 12:36:40] --- [traceNum] ---- 4
[2018-06-11 오후 12:36:40] message:
[2018-06-11 오후 12:36:40] member name: btn_Click
[2018-06-11 오후 12:36:40] source file path: c:\users\admin\source\repos\WpfApp25\WpfApp25\MainWindow.xaml.cs
[2018-06-11 오후 12:36:40] source line number: 33
[2018-06-11 오후 12:36:40] --- [traceNum] ---- 5
[2018-06-11 오후 12:36:40] message:
[2018-06-11 오후 12:36:40] member name: btn_Click
[2018-06-11 오후 12:36:40] source file path: c:\users\admin\source\repos\WpfApp25\WpfApp25\MainWindow.xaml.cs
[2018-06-11 오후 12:36:40] source line number: 33
참고
TraceMessage 관련 : http://codingcoding.tistory.com/49?category=735724
https://msdn.microsoft.com/ko-kr/library/system.runtime.compilerservices(v=vs.110).aspx
이런 함수가 있는줄도 몰랐다.
우연히 발견했지만 넘넘 감사합니다^^