Computer

[C#] log 저장

창천(蒼天) 2013. 5. 24. 17:38


        private void SaveLog(string log)
        {
            // Log 저장될 경로
            m_logDicPath = string.Format("{0}\\log", Application.StartupPath);
            // Log 파일 이름 (로그가 생성된 일자를 기준으로 생성되게)
            m_logFilePath = string.Format("{0}\\{1}_log.txt", m_logDicPath, DateTime.Today.ToShortDateString());

            // 기록될 Message를 만든다.
            string msg = string.Format("[{0}] {1}{2}", DateTime.Now.ToString("u"), log, Environment.NewLine);

          // Log를 기록할 폴더 유무를 확인후 생성한다
            if (!Directory.Exists(m_logDicPath))
            {
                Directory.CreateDirectory(m_logDicPath);
            }

            // Log 내용을 text 파일에 기록한다.
            File.AppendAllText(m_logFilePath, msg);
        }