封装一个C#日志类Loger

 1     public class Loger
 2     {
 3         /// <summary>
 4         /// 写入日志
 5         /// </summary>
 6         /// <param name="content">日志内容</param>
 7         /// <param name="title">日志标题</param>
 8         /// <param name="folderName">文件夹名称</param>
 9         /// <param name="filePrefixName">文件前缀名</param>
10         public static void Write(string content, string title = "", string folderName = "Log", string filePrefixName = "Log")
11         {
12             try
13             {
14                 lock (typeof(Loger))
15                 {
16                     DateTime dateTimeNow = DateTime.Now;
17                     string logDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", folderName);
18                     if (!Directory.Exists(logDirPath))
19                     {
20                         Directory.CreateDirectory(logDirPath);
21                     }
22 
23                     string logFilePath = string.Format("{0}/{1}-{2}.txt", logDirPath, filePrefixName, dateTimeNow.ToString("yyyy-MM-dd"));
24                     using (StreamWriter writer = new StreamWriter(logFilePath, true, Encoding.UTF8))
25                     {
26                         try
27                         {
28                             writer.WriteLine("------------------------------------------------------------------------------------------");
29                             writer.WriteLine(title);
30                             writer.WriteLine("日志时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
31                             writer.WriteLine(content);
32                             writer.WriteLine("------------------------------------------------------------------------------------------");
33                         }
34                         catch (Exception ex)
35                         {
36                             Console.WriteLine("Loger.cs Line45" + ex.Message);
37                         }
38 
39                         writer.Close();
40                     }
41                 }
42             }
43             catch(Exception ex)
44             {
45                 Console.WriteLine("Loger.cs Line54" + ex);
46                 //throw new Exception("无法将日志写入文件,请查看安装目录是否有权限!");
47             }
48         }
49 
50         /// <summary>
51         /// 写入日志
52         /// </summary>
53         /// <param name="format">符合格式字符串</param>
54         /// <param name="args">一个对象数组,其中包含零个或多个要设置格式的对象</param>
55         public static void WriteFormat(string format, params object[] args)
56         {
57             string content = string.Format(format, args);
58             Write(content, "", "Log", "Log");
59         }
60 
61         /// <summary>
62         /// 写入日志
63         /// </summary>
64         /// <param name="ex">Exception对象</param>
65         /// <param name="title">日志标题</param>
66         /// <param name="folderName">文件夹名称</param>
67         /// <param name="filePrefixName">文件前缀名</param>
68         public static void Write(Exception ex, string title = "", string folderName = "Exception", string filePrefixName = "Exception")
69         {
70             string content = string.Format("错误信息:{1}{0}错误来源:{2}{0}堆栈信息:{0}{3}", Environment.NewLine, ex.Message, ex.Source, ex.StackTrace);
71             Write(content, title, folderName, filePrefixName);
72         }
73     }

 

posted @ 2018-03-22 14:38  liu_xh  阅读(1458)  评论(0编辑  收藏  举报