写Log日志的方法 减少插件引用

 1 public class Log
 2 {
 3 /// <summary>
 4 /// 记录状态
 5 /// </summary>
 6 /// <param name="str">信息内容</param>
 7 public static void WriteLog(string Message)
 8 {
 9 try
10 {
11 string path = System.Windows.Forms.Application.StartupPath;
12 DateTime dt = DateTime.Now;
13 string str = Message;
14 if (!Directory.Exists(path + @"\ErrLog"))
15 Directory.CreateDirectory(path + @"\ErrLog");
16 using (var sw = new StreamWriter(path + @"\ErrLog\" + (dt.Year + "-" + dt.Month + "-" + dt.Day).ToString() + "_InfoLog.log", true))
17 {
18 sw.WriteLine(dt.ToString() + "---------------------------------------------------------");
19 sw.WriteLine(str);
20 sw.WriteLine("---------------------------------------------------------");
21 sw.Close();
22 }
23 }
24 catch (Exception)
25 {
26 
27 }
28 }
29 
30 /// <summary>
31 /// 将异常打印到LOG文件
32 /// </summary>
33 /// <param name="ex">异常</param>
34 /// <param name="LogAddress">日志文件地址</param>
35 public static void WriteLog(Exception ex)
36 {
37 try
38 {
39 string path = System.Windows.Forms.Application.StartupPath;
40 //在Debug目录下新建 YYYY-mm-dd_Log.log文件
41 if (!Directory.Exists(path + @"\ErrLog"))
42 Directory.CreateDirectory(path + @"\ErrLog");
43 DateTime dt = DateTime.Now;
44 //把异常信息输出到文件
45 StreamWriter sw = new StreamWriter(
46 path + @"\ErrLog\" +
47 (dt.Year + "-" + dt.Month + "-" + dt.Day).ToString() +
48 "_ErrorLog.log", true);
49 sw.WriteLine("当前时间:" + dt.ToString());
50 sw.WriteLine("异常信息:" + ex.Message);
51 sw.WriteLine("异常对象:" + ex.Source);
52 sw.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
53 sw.WriteLine("触发方法:" + ex.TargetSite);
54 sw.WriteLine("---------------------------------------------------------");
55 sw.WriteLine();
56 sw.Close();
57 }
58 catch (Exception)
59 {
60 
61 }
62 }
63 }

 

posted @ 2020-07-24 10:33  Sorry名米  阅读(128)  评论(0编辑  收藏  举报