码迷,mamicode.com
首页 > 其他好文 > 详细

一个简易的日志程序

时间:2015-05-03 00:45:26      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

      在程序开发过程中,我们经常要记录各种操作信息以及系统信息到日志中,以便于以后查找相关记录或者在遇到程序出现错误时查找错误的原因。一般日志存储于两种介质中:一、存储入数据库,另一种存储于文本文档中。我们最常用的插件有log4.net等。但是对我们日常的小程序来说,它可能过重,所以我自己在自己的开发中写了一个简单的小程序以适应小程序的需要。

为方便自己以后查找,记录代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Web;
 8 
 9 
10 
11 namespace IST.Common.Utility
12 {
13     public class LogManager
14     {
15         /// <summary>
16         /// 当前目录路径
17         /// </summary>
18         public static string strPath;
19         //public static string strPath = HttpContext.Current.Server.MapPath("\\Log"); //System.Environment.CurrentDirectory;
20 
21         #region      判断是否存在日志文件夹,没有则创建一个
22         /// <summary>
23         /// 判断是否存在日志文件夹,没有则创建一个
24         /// </summary>
25         private static string ExsitsDirectory(LogType type)
26         {
27             string directoryName = strPath + "\\" + type.ToString();
28             try
29             {
30                 if (!Directory.Exists(directoryName))
31                 {
32                     Directory.CreateDirectory(directoryName);
33                 }
34             }
35             catch (Exception ex)
36             {
37 
38             }
39             return directoryName;
40         }
41         #endregion
42 
43 
44         #region  把信息写入日志文件
45         /// <summary>
46         /// 把信息写入日志文件
47         /// </summary>
48         /// <param name="errorMsg">日志文本</param>
49         /// <param name="type">日志类型</param>
50         public static void WriteLog(string errorMsg,LogType type)
51         {
52             try
53             {
54                 string directoryName = ExsitsDirectory(type);
55                 string fileName = directoryName + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
56                 if (!File.Exists(fileName))
57                 {
58                     FileStream fs = new FileStream(fileName, FileMode.Create);
59                     fs.Close();
60                 }
61                 StreamWriter sw = File.AppendText(fileName);
62                 sw.WriteLine("==========" + DateTime.Now.ToString() + "     " + errorMsg + "\n");
63                 sw.Close();
64             }
65             catch
66             {
67 
68             }
69         }
70         #endregion
71     }
72 
73     /// <summary>
74     /// 日志类型,用于区分日志应写入相应的文件中
75     /// </summary>
76     public enum LogType
77     {
78         /// <summary>
79         /// Bug
80         /// </summary>
81         Bug,
82         /// <summary>
83         /// 错误
84         /// </summary>
85         Error,
86         /// <summary>
87         /// 信息
88         /// </summary>
89         Info
90     }
91 }

 

一个简易的日志程序

标签:

原文地址:http://www.cnblogs.com/feixufengxiang/p/4472725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!