关于C#异常Log处理

 

using System.Windows.Forms;
using System.IO;

public static void CreateLog(Exception ex)
{
string path = Application.StartupPath +"\\Log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "\\" + DateTime.Now.Month +"_"+ DateTime.Now.Day +"_"+ DateTime.Now.Hour+DateTime.Now.Minute+".txt";
WriteL(path, ex);
}

 

 


public static void WriteL(string path , Exception ex)
{
using (StreamWriter sw = new StreamWriter(path,false,Encoding.UTF8))
{
sw.WriteLine("*********************"
+DateTime.Now.ToString()
+"*********************");
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);//通过堆栈来追溯异常
}
else
sw.WriteLine("Exception is null");
sw.WriteLine();
}

}

posted @ 2015-05-14 11:04  shuke2016  阅读(569)  评论(0编辑  收藏  举报