当前位置: 代码迷 >> .NET相关 >> .net 创办属于自己的log类
  详细解决方案

.net 创办属于自己的log类

热度:428   发布时间:2016-04-24 02:39:23.0
.net 创建属于自己的log类

实习到现在已经接近三个月了,由于是校企联合培养计划,所以没有工资,所幸公司对于我们这些实习生并没有什么要求,刚开始我还觉得要做点什么才能学得快,可是到了后来,发现公司安排给我们的任务并不紧要,也不算太难。

到现在大约接触了2,3个模块的设计和实现了,起初,在文件读写和记录日志方面屡屡收到了批评,因外在大学的时候并没有记录日子的习惯,使得这个时候匆匆忙忙要养成在关键位置记录日志的习惯变得有些困难,

起初我是使用了postsharp的第三方插件来记录自己的运行日志的,可是后来发现在代码移植方面,如果其他电脑没有安装postsharp会使得移植过程变得复杂,特别是在部署的时候增加复杂度。

现在在进行新的模块的时候决定不再使用postsharp了,打算自己写一个简单的记录日志的类,来记录在关键位置的操作。

 

不废话,直接出代码,代码比较简单,实现的功能也非常简单。

可以直接复制粘贴来使用,同时,我也提供了源码给像我一样的初学者下载。

 

 1 using System; 2 using System.IO; 3  4 namespace CustomerLog 5 { 6  7     /// <summary> 8     /// 保存日志 9     /// </summary>10     public class Logging11     {12         #region 日志分类13         /// <summary>14         /// 保存普通日志15         /// </summary>16         /// <param name="message"></param>17         public static void Writelog(string message)18         {19             string logContent = string.Format("[{0}] =>{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), message);20             SetFile(@"Log.txt", logContent);21         }22 23         /// <summary>24         /// 保存关键日志25         /// </summary>26         /// <param name="message"></param>27         public static void WriteKeylog(string message)28         {29             var logContent = string.Format("[{0}]=>{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), message);30             SetFile(@"KeyLog.txt", logContent);31         }32 33         /// <summary>34         /// 保存错误信息日志35         /// </summary>36         /// <param name="ex"></param>37         public static void WriteBuglog(Exception ex)38         {39             var logContent = string.Format("[{0}]错误发生在:{1},\r\n 内容:{2}",40                 DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ex.Source, ex.Message);41             logContent += string.Format("\r\n [{0}] 跟踪:{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),42                 ex.StackTrace);43             SetFile(@"BugLog.txt", logContent);44         }45         #endregion46 47         #region 通用操作48         /// <summary>49         /// 标准化写入过程,继承之后可自定义写入内容50         /// 默认保存在debug目录的Log目录下51         /// </summary>52         /// <param name="filename">文件名</param>53         /// <param name="logContent">写入内容</param>54         protected static void SetFile(string filename, string logContent)55         {56             Isexist(); // 判断Log目录是否存在57             string errLogFilePath = Environment.CurrentDirectory + @"\Log\" + filename.Trim();58             StreamWriter sw;59             if (!File.Exists(errLogFilePath))60             {61                 FileStream fs1 = new FileStream(errLogFilePath, FileMode.Create, FileAccess.Write);62                 sw = new StreamWriter(fs1);63             }64             else65             {66                 sw = new StreamWriter(errLogFilePath, true);67             }68             sw.WriteLine(logContent);69             sw.Flush();70             sw.Close();71         }72 73         // 判断是否存在日志文件74         private static void Isexist()75         {76             string path = Environment.CurrentDirectory + @"\Log\";77             if (!File.Exists(path))78             {79                 Directory.CreateDirectory(path);80             }81         }82         #endregion83     }84 }

 

 

                        ↓↓↓↓↓下载地址↓↓↓↓↓【目前只会用百度云盘分享,求大神教授更加好用的分享方式】

               http://pan.baidu.com/s/1kTNcfdH

3楼吉翁残党
菜鸟问一下,如果频繁写入日志,不会阻塞吗
2楼cocosip
这个太粗糙了。而且会有资源竞争与阻塞问题
1楼十※年十※年
直接log4net多好~没必要自己写~自己写可能出现各种问题
  相关解决方案