日志记录到记事本

记录日志是常用的,以下是一个在指定目录下面按天穿件日志文件的公共类:

 1 /// <summary>
 2     /// 公用日志
 3     /// </summary>
 4     public class Log
 5     {
 6         private static string path = Environment.CurrentDirectory + @"/logs";
 7 
 8         public static void WriteLog(string functionName, Dictionary<string, string> param,double seconds)
 9         {
10             if (!Directory.Exists(path))//如果日志目录不存在就创建
11             {
12                 Directory.CreateDirectory(path);
13             }
14             string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
15             string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
16                                                                                         //创建或打开日志文件,向日志文件末尾追加记录
17             StreamWriter mySw = File.AppendText(filename);
18 
19             //向日志文件写入内容
20             StringBuilder write_content = new StringBuilder("/**************************************/
记录日志时间:" + time + "
执行方法:" + functionName + "
执行耗时:" + seconds+"");
21             foreach (string item in param.Keys)
22             {
23                 write_content.Append(string.Format("{0}:{1}
",item, param[item]));
24             }
25             write_content.Append("/**************************************/
");
26             mySw.WriteLine(write_content);
27 
28             //关闭日志文件
29             mySw.Close();
30         }
31     }
原文地址:https://www.cnblogs.com/CarlBlogs/p/7404981.html