码迷,mamicode.com
首页 > Windows程序 > 详细

C#输出log信息

时间:2015-04-14 19:25:54      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

在写程序的过程中,有时候我们需要添加一些log信息,这个时候,可以采用下面的方法来实现。

技术分享
        public static void WriteLog(string ExtraMsg, Exception e)
        {
           string logPath = Environment.CurrentDirectory + "\\Log";
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

           string logFilePath = Path.Combine(logPath, string.Format("{0}.log", DateTime.Now.Date.ToString("yyyyMMdd")));
           if (e is System.Threading.ThreadAbortException)
               return;

           string strMsg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "   " + e.Message;
           string detailMsg = string.Format("类型:{0}\r\n堆栈:{1})", e.GetType().Name, e.StackTrace);
           using (StreamWriter sw = new StreamWriter(logFilePath, true))
           {
               sw.WriteLine(strMsg);
               sw.WriteLine(ExtraMsg);
               sw.WriteLine(detailMsg);
               sw.WriteLine();
               sw.Close();
           }
        }
WriteLog

 

C#输出log信息

标签:

原文地址:http://www.cnblogs.com/ningheshutong/p/4425799.html

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