본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - 로그파일 만들기 ( LOG )

728x90
반응형

 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 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)
    {
    }
}
cs




사용하기



// 프로그램 실행
1
LogWrite(AppDomain.CurrentDomain.FriendlyName + "실행");
cs




728x90
반응형