예전 메일 프로젝트 하면서 간단히 만들었던 건데
참조용으로 그냥 올려본다.
메인폼의 리스트박스에도 해당 내용을 보여주고, 로그저장 폴더에 파일로 하루 단위로 저장하게끔 되어 있다.
로그폴더를 현재 실행되는 애플리케이션 하위폴더에 생성할려면
strLogDirectory = @Application.StartupPath + "\\Log\\";
if (!Directory.Exists(strLogDirectory))
{
Directory.CreateDirectory(strLogDirectory);
}
위와 같이 미리 초기화할때 설정해두면 된다.
private void writeLog(string strLog, int logLevel)
{
if (lbLogInfo.Items.Count > 100)
{
lbLogInfo.Items.Clear();
}
lbLogInfo.Items.Add(DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToLongTimeString() + " " + strLog);
if (logLevel <= msgLogLevel)
{
string Filename = strLogDirectory + "MailLog_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".txt";
StreamWriter sw = null;
try
{
sw = new StreamWriter(Filename, true, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " " + strLog); //
sw.Flush();
}
finally
{
if (sw != null) sw.Close();
sw = null;
}
}
}