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

错误日志类C#

时间:2014-05-22 05:08:36      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   java   

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WisdomCity.Entitys
{
    public static class ErrorLog
    {
        /// <summary>
        /// 创建日志文件
        /// </summary>
        /// <param name="ex">异常类</param>
        public static void CreateLog(Exception ex, string param)
        {
            string path = Application.StartupPath + "\\ErrorLog";
            if (!Directory.Exists(path))
            {
                //创建日志文件夹
                Directory.CreateDirectory(path);
            }
            //发生异常每天都创建一个单独的日子文件[*.log],每天的错误信息都在这一个文件里。方便查找
            //path += "\\" + DateTime.Now.ToShortDateString() + ".log";
            path += "\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            WriteLogInfo(ex, path, param);
        }
        /// <summary>
        /// 写日志信息
        /// </summary>
        /// <param name="ex">异常类</param>
        /// <param name="path">日志文件存放路径</param>
        private static void WriteLogInfo(Exception ex, string path, string param)
        {
            using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
            {
                sw.WriteLine("*****************************************【"
                               + DateTime.Now.ToLongTimeString()
                               + "】*****************************************");
                if (ex != null)
                {
                    sw.WriteLine("【ErrorType】" + ex.GetType());
                    sw.WriteLine("【TargetSite】" + ex.TargetSite);
                    sw.WriteLine("【Message】" + ex.Message);
                    sw.WriteLine("【Source】" + ex.Source);
                    sw.WriteLine("【StackTrace】" + ex.StackTrace);
                    sw.WriteLine("【param】" + param);
                }
                else
                {
                    sw.WriteLine("Exception is NULL");
                }
                sw.WriteLine();
            }
        }


    }
}
bubuko.com,布布扣

调用示例:

bubuko.com,布布扣
try
            {//错误检出使程序继续进行
////

}
            catch (Exception ex)
            {
                ErrorLog.CreateLog(ex, "Function:AddBedditPreMessge;" + strUname + ";" + strUid + ";" + sleepPreId +";"+ items);
                throw;
            }
bubuko.com,布布扣

 

 

错误日志类C#,布布扣,bubuko.com

错误日志类C#

标签:style   blog   class   c   code   java   

原文地址:http://www.cnblogs.com/nidakun/p/3738328.html

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