写日志通用,winForm和web程序都好使

/// 
        /// 写日志
        /// 
        /// 
        private void WriteLog(string Descript)
        {
            try
            {
                //日志
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs\\");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                DateTime danow = DateTime.Now;


                string FullFilePath = Path.Combine(path, danow.ToString("yyyy-MM-dd_HH") + "_log.txt");

                StringBuilder str = new StringBuilder();
                str.Append(danow.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-->");
                str.Append(Descript);

                StreamWriter sw;
                if (File.Exists(FullFilePath))
                {
                    sw = File.AppendText(FullFilePath);
                }
                else
                {
                    sw = File.CreateText(FullFilePath);
                }
                sw.WriteLine(str.ToString());
                sw.Close();
            }
            catch { }
        }

 

你可能感兴趣的:(C#)