tanhu

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
记得下次开发程序是一定要书写日志,这样可以避免很多错误,可以说清楚不是自己的问题。



using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CACPayLib { public class Log { //在网站根目录下创建日志目录 public static string path = System.Environment.CurrentDirectory+ "\\PayLogs\\logs"; /** * 向日志文件写入调试信息 * @param className 类名 * @param content 写入内容 */ public static void Debug(string className, string content) { if (CInfConfig.LOG_LEVENL >= 3) { WriteLog("DEBUG", className, content); } } /** * 向日志文件写入运行时信息 * @param className 类名 * @param content 写入内容 */ public static void Info(string className, string content) { if (CInfConfig.LOG_LEVENL >= 2) { WriteLog("INFO", className, content); } } /** * 向日志文件写入出错信息 * @param className 类名 * @param content 写入内容 */ public static void Error(string className, string content) { if (CInfConfig.LOG_LEVENL >= 1) { WriteLog("ERROR", className, content); } } /** * 实际的写日志操作 * @param type 日志记录类型 * @param className 类名 * @param content 写入内容 */ protected static void WriteLog(string type, string className, string content) { if (!Directory.Exists(path))//如果日志目录不存在就创建 { Directory.CreateDirectory(path); } string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间 string filename = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名 //创建或打开日志文件,向日志文件末尾追加记录 StreamWriter mySw = File.AppendText(filename); //向日志文件写入内容 string write_content = time + " " + type + " " + className + ": " + content; mySw.WriteLine(write_content); //关闭日志文件 mySw.Close(); } } }

 

posted on 2018-05-16 14:00  tanhu  阅读(218)  评论(0编辑  收藏  举报