프로그램 실행시 bin 폴더에 에러로그 텍스트 파일로 남기는 꿀팁이다.
에러발생시 DB로 쏴줘도 되지만 bin폴더에 텍스트로 남겨야 하는 특수한 상황도 존재한다..
catch 단에 아래 메서드를 호출해서 사용하면 된다.
private static void fn_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)
{
}
}
'C#' 카테고리의 다른 글
C# Summary 기능 활용법 (꿀팁) (1) | 2023.12.13 |
---|---|
C# 윈도우 파일명 유효성 체크 및 변경 (1) | 2023.12.06 |
.Net SMTP 메일 전송(Outlook)시 이미지 깨짐 현상 해결 방법 (0) | 2023.12.06 |
[Winform, C#] Process.Start() 이용하여 새창(새탭X) 에서 브라우저 열기 (0) | 2023.01.30 |
댓글