程序中编写log日志

  1 public string logFile;
  2         private int configLogLevel = 0;
  3         private Stream s = null;
  4         StreamWriter sw = null;
  5 
  6         /// <summary>
  7         /// 用log文件名初始化对象
  8         /// </summary>
  9         /// <param name="logFile"></param>
 10         public WriteLog(string logFile)
 11         {
 12             this.logFile = logFile;
 13             initFile();
 14         }
 15 
 16         /// <summary>
 17         /// 用log文件名和配置的log级别初始化对象
 18         /// </summary>
 19         /// <param name="logFile"></param>
 20         /// <param name="configLevel"></param>
 21         public WriteLog(string logFile,int configLevel)
 22         {
 23             this.logFile = logFile;
 24             this.configLogLevel = configLevel;
 25             initFile();
 26         }
 27 
 28         ~WriteLog()
 29         {
 30             //析构函数,释放资源
 31             //if (sw != null)
 32             //{
 33             //    sw.Flush();
 34             //    sw.Close();
 35             //}
 36             //if (s != null)
 37             //{
 38             //    s.Flush();
 39             //    s.Close();
 40             //}
 41         }
 42 
 43         /// <summary>
 44         /// 初始化文件流
 45         /// </summary>
 46         private void initFile()
 47         {
 48             try
 49             {
 50                 if (this.logFile == null || this.logFile == "") return;
 51 
 52                 string path = "";
 53                 string fileName = "";
 54                 if (this.logFile.IndexOf("\\") >= 0)
 55                 {
 56                     path = this.logFile.Substring(0,this.logFile.LastIndexOf("\\")+1);
 57                     fileName = this.logFile.Substring(this.logFile.LastIndexOf("\\")+1);
 58                 }
 59                 else if (this.logFile.IndexOf("/") >= 0)
 60                 {
 61                     path = this.logFile.Substring(0, this.logFile.LastIndexOf("/")+1);
 62                     fileName = this.logFile.Substring(this.logFile.LastIndexOf("/") + 1);
 63                 }
 64                 else
 65                 {
 66                     return;
 67                 }
 68                 string dateStr = DateTime.Now.ToString("yyyyMMdd");
 69                 path += dateStr;
 70                 if (!Directory.Exists(path))
 71                 {
 72                     Directory.CreateDirectory(path);
 73                 }
 74                 fileName = path + "\\"+ fileName;
 75 
 76                 if (File.Exists(fileName))
 77                 {
 78                     s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
 79                 }
 80                 else
 81                 {
 82                     //CommonFunction.createDir(this.logFile.Substring(0, this.logFile.LastIndexOf(@"\"))); 
 83                     s = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
 84                     s.Flush();
 85                     s.Close();
 86                     s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
 87                 }
 88 
 89                 sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
 90                 //sw.AutoFlush = true;
 91             }
 92             catch (Exception ex)
 93             {
 94                 throw ex;
 95             }
 96         }
 97 
 98         /// <summary>
 99         /// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
100         /// </summary>
101         /// <param name="logLevel">当前log的级别</param>
102         /// <param name="configLevel">配置设定的log级别</param>
103         /// <param name="logContent">log内容</param>
104         //[MethodImpl(MethodImplOptions.Synchronized)]
105         public void log(int logLevel,int configLevel,string logContent)
106         {
107             //如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
108             if (logLevel < configLevel && sw != null && s != null)
109             {
110                 try
111                 {
112                     lock (sw)
113                     {
114                         sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLevel + " ;" + logContent);
115                         sw.Flush();
116                         //s.Flush();
117                     }
118                 }
119                 catch (Exception ex)
120                 {
121                     ex.ToString();
122                 }
123                 
124             }
125         }
126 
127         /// <summary>
128         /// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
129         /// </summary>
130         /// <param name="logLevel">当前log的级别</param>
131         /// <param name="logContent">log内容</param>
132         //[MethodImpl(MethodImplOptions.Synchronized)]
133         public void log(int logLevel, string logContent)
134         {
135             //如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
136             if (logLevel < this.configLogLevel && sw != null && s != null)
137             {
138                 try
139                 {
140                     lock (sw)
141                     {
142                         sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;" + logContent);
143                         sw.Flush();
144                         //s.Flush();
145                     }
146                 }
147                 catch (Exception ex)
148                 {
149                     ex.ToString();
150                 }
151                 
152             }
153         }
154 
155         /// <summary>
156         /// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
157         /// </summary>
158         /// <param name="logLevel">当前log的级别</param>
159         /// <param name="logContent">log内容</param>
160         /// <param name="method">log抛出调用的方法名</param>
161         //[MethodImpl(MethodImplOptions.Synchronized)]
162         public void log(int logLevel, string logContent,string methodName)
163         {
164             //如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
165             if (logLevel < this.configLogLevel && sw != null && s != null)
166             {
167                 try
168                 {
169                     lock (sw)
170                     {
171                         sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;method=" + methodName + ";content=" + logContent);
172                         sw.Flush();
173                         //s.Flush();
174                     }
175                 }
176                 catch (Exception ex)
177                 {
178                     ex.ToString();
179                 }
180             }
181         }
182 
183         public void close()
184         {
185             try
186             {
187                 if (sw != null)
188                 {
189                     sw.Flush();
190                     sw.Close();
191                 }
192                 if (s != null)
193                 {
194                     s.Flush();
195                     s.Close();
196                 }
197             }
198             catch (Exception ex)
199             {
200                 ex.ToString();
201             }
202         }
View Code

 

posted @ 2013-12-20 16:30  hongsedigua  阅读(443)  评论(0编辑  收藏  举报