WPF & C# - 로그파일 만들기 ( LOG )
카테고리 없음2018. 4. 16. 11:33
WPF & C# - 로그파일 만들기 ( LOG ) |
MainWindows.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 | private void btn_input_Click(object sender, RoutedEventArgs e) { LogWrite("버튼클릭"); } private void LogWrite(string str) { string FilePath = Environment.CurrentDirectory + @"\Log\Log_" + DateTime.Today.ToString("yyyyMMdd") + ".log"; string DirPath = Environment.CurrentDirectory + @"\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) { } } | cs |