c#写 log方法

        public static  void WriteLogToFileDiffTable(string msg)
        {
            string strDic = System.AppDomain.CurrentDomain.BaseDirectory;
            IniMen readIdent = new IniMen();
            readIdent.LoadFromFile(strDic + "service.ini");
            string logFile = readIdent.ReadIdent("configSet", "selectLogFilePath", "");
            System.DateTime currentTime = System.DateTime.Now;

            if (!Directory.Exists(logFile + "\\logFiles"))  //不存在则创建
            {
                Directory.CreateDirectory(logFile + "\\logFiles");
            }
            string logPath = logFile + "\\logFiles\\IMPORT_DTJC_LOG_DIFF_" + currentTime.ToLongDateString() + ".txt";
            try
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + currentTime.ToString() + "]" + msg);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException e)
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "]" + e.Message);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/guoruijun_2012_4/article/details/85341826