一个日志类 LogUtil

第一次自主封装Util类,尝试封装一个日志的操作类LogUtil,逻辑不复杂,也没调用那些复杂的类,只是简单的IO操作。

  1 using System;
  2 using System.IO;
  3 
  4 namespace Common
  5 {
  6     /// <summary>
  7     /// 日志类
  8     /// </summary>
  9     public static class LogUtil
 10     {
 11         /// <summary>
 12         /// 配置默认路径
 13         /// </summary>
 14         private static string defaultPath = System.Configuration.ConfigurationManager.AppSettings["logPath"];
 15 
 16         #region Exception异常日志
 17 
 18         /// <summary>
 19         /// 写异常日志,存放到默认路径
 20         /// </summary>
 21         /// <param name="ex">异常类</param>
 22         public static void WriteError(Exception ex)
 23         {
 24             WriteError(ex, defaultPath);
 25         }
 26 
 27         /// <summary>
 28         /// 写异常日志,存放到指定路径
 29         /// </summary>
 30         /// <param name="ex">异常类</param>
 31         /// <param name="path">日志存放路径</param>
 32         public static void WriteError(Exception ex, string path)
 33         {
 34             string errMsg = CreateErrorMeg(ex);
 35             WriteLog(errMsg, path, LogType.Error);
 36         }
 37 
 38         #endregion
 39 
 40         #region 普通日志
 41 
 42         /// <summary>
 43         /// 写普通日志,存放到默认路径,使用默认日志类型
 44         /// </summary>
 45         /// <param name="msg">日志内容</param>
 46         public static void WriteLog(string msg)
 47         {
 48             WriteLog(msg, LogType.Info);
 49         }
 50 
 51         /// <summary>
 52         /// 写普通日志,存放到默认路径,使用指定日志类型
 53         /// </summary>
 54         /// <param name="msg">日志内容</param>
 55         /// <param name="logType">日志类型</param>
 56         public static void WriteLog(string msg, LogType logType)
 57         {
 58             WriteLog(msg, defaultPath, logType);
 59         }
 60 
 61         /// <summary>
 62         /// 写普通日志,存放到指定路径,使用默认日志类型
 63         /// </summary>
 64         /// <param name="msg">日志内容</param>
 65         /// <param name="path">日志存放路径</param>
 66         public static void WriteLog(string msg, string path)
 67         {
 68             WriteLog(msg, path, LogType.Info);
 69         }
 70 
 71         /// <summary>
 72         /// 写普通日志,存放到指定路径,使用指定日志类型
 73         /// </summary>
 74         /// <param name="msg">日志内容</param>
 75         /// <param name="path">日志存放路径</param>
 76         /// <param name="logType">日志类型</param>
 77         public static void WriteLog(string msg, string path, LogType logType)
 78         {
 79             string fileName = path.Trim('\\')+"\\"+CreateFileName(logType);
 80             string logContext = FormatMsg(msg,logType);
 81             WriteFile(logContext, fileName);
 82         }
 83 
 84         #endregion
 85 
 86         #region 其他辅助方法
 87 
 88         /// <summary>
 89         /// 写日志到文件
 90         /// </summary>
 91         /// <param name="logContext">日志内容</param>
 92         /// <param name="fullName">文件名</param>
 93         private static void WriteFile(string logContext, string fullName)
 94         {
 95             FileStream fs = null;
 96             StreamWriter sw = null;
 97 
 98             int splitIndex = fullName.LastIndexOf('\\');
 99             if (splitIndex == -1) return;
100             string path = fullName.Substring(0, splitIndex);
101             
102             if (!Directory.Exists(path)) Directory.CreateDirectory(path);
103 
104             try
105             {
106                 if (!File.Exists(fullName)) fs = new FileStream(fullName, FileMode.CreateNew);
107                 else fs = new FileStream(fullName, FileMode.Append);
108 
109                 sw = new StreamWriter(fs);
110                 sw.WriteLine(logContext);
111             }
112             finally
113             {
114                 if (sw != null)
115                 {
116                     sw.Close();
117                     sw.Dispose();
118                 }
119                 if (fs != null)
120                 {
121                     fs.Close();
122                     fs.Dispose();
123                 }
124             }
125         }
126 
127         /// <summary>
128         /// 格式化日志,日志是默认类型
129         /// </summary>
130         /// <param name="msg">日志内容</param>
131         /// <returns>格式化后的日志</returns>
132         private static string FormatMsg(string msg)
133         {
134             return FormatMsg(msg, LogType.Info);
135         }
136 
137         /// <summary>
138         /// 格式化日志
139         /// </summary>
140         /// <param name="msg">日志内容</param>
141         /// <param name="logType">日志类型</param>
142         /// <returns>格式化后的日志</returns>
143         private static string FormatMsg(string msg, LogType logType)
144         {
145             string result;
146             string header = string.Format("[{0}][{1} {2}] ", logType.ToString(), DateTime.Now.ToShortDateString(),DateTime.Now.ToShortTimeString());
147             result= header + msg;
148             return result;
149         }
150 
151         /// <summary>
152         /// 从异常类中获取日志内容
153         /// </summary>
154         /// <param name="ex">异常类</param>
155         /// <returns>日志内容</returns>
156         private static string CreateErrorMeg(Exception ex)
157         {
158             string result = string.Empty;
159             result += ex.Message+"\r\n";
160             result += ex.StackTrace+"\r\n";
161             return result;
162         }
163 
164         /// <summary>
165         /// 生成日志文件名
166         /// </summary>
167         /// <param name="logType">日志类型</param>
168         /// <returns>日志文件名</returns>
169         private static string CreateFileName(LogType logType)
170         {
171             string result = DateTime.Now.ToString("yyyy-MM-dd");
172 if (logType != LogType.Info) 173 result = logType.ToString() + result+".log"; 174 return result; 175 } 176 177 #endregion 178 } 179 180 /// <summary> 181 /// 日志类型 182 /// </summary> 183 public enum LogType 184 { 185 Error, 186 Info, 187 Option 188 } 189 }

 

posted @ 2012-12-31 17:08  猴健居士  阅读(4897)  评论(2编辑  收藏  举报