导航

错误日志编写

Posted on 2017-08-04 21:58  清浅ヾ  阅读(127)  评论(0编辑  收藏  举报
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace Logs
10 {
11     public class ErrorLogs
12     {
13         public static void Write(string path,string message, Exception ex)
14         {
15             if (path != null && path != "")
16             {
17                 if (!(Directory.Exists(path)))
18                 {
19                     Directory.CreateDirectory(path); //创建日志文件夹
20                 }
21                 path += string.Format(@"\\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
22 
23                 using (var sw = new StreamWriter(path, true, Encoding.Default))
24                 {
25                     sw.WriteLine("***********************************************************************************************");
26                     sw.WriteLine(DateTime.Now.ToString());
27                     if (ex != null)
28                     {
29                         sw.WriteLine("异常信息:");
30                         sw.WriteLine(message);
31                         sw.WriteLine("【Message】" + ex.Message);
32                         sw.WriteLine("【ErrorType】:" + ex.GetType());
33                         sw.WriteLine("【TargetSite】" + ex.TargetSite);
34                         sw.WriteLine("【StackTrace】" + ex.StackTrace);
35                     }
36                     else sw.WriteLine("Exception Is Null");
37                     sw.WriteLine();
38                 }
39             }
40         }
41     }
42 }