using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// ExceptionBase 的摘要说明
/// </summary>
public class ExceptionBase
{
public ExceptionBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//private static readonly string LogPath = ConfigurationSettings.AppSettings["LogPath"];
//private static readonly string Isdebug = ConfigurationSettings.AppSettings["Isdebug"];
private static readonly string LogPath =@"E:/huitu";
private static readonly string Isdebug ="0";
/// <summary>
/// 对应用程序发生的异常的控制方法
/// 作者:Robert 2007-9-24 13:48:00
/// </summary>
/// <param name="e"> </param>
/// <param name="user"> </param>
public static void ExceptionControl(Exception e, string user,string url)
{
//string [] errlist = e.StackTrace.Split("在");
if (Isdebug == "1") //本地测试
{
throw new ApplicationException(e.Message, e);
}
else //正式发布的时候
{
WriteLog("类型:" + e.GetType().FullName + "/t方法:"
+ e.TargetSite.ToString() + "/t对象名称:" + e.Source + "/t消息:" + e.Message, user,e.StackTrace);
}
}
/// <summary>
/// 对Sql发生的异常的控制方法
/// 作者:Robert 2007-9-24 13:48:00
/// </summary>
/// <param name="e"> </param>
/// <param name="user"> </param>
public static void ExceptionControl(SqlException e, string user)
{
if (Isdebug == "1") //本地测试
{
throw new ApplicationException(e.Message, e);
}
else //正式发布的时候
{
WriteLog("类型:" + e.GetType().FullName + "/t方法:"
+ e.TargetSite.ToString() + "/t对象名称:" + e.Source + "/t消息:" + e.Message, user, e.TargetSite.ReflectedType.ToString());
}
}
/// <summary>
/// 对异常写日志
/// 作者:Robert 2007-9-24 11:20:00
/// </summary>
/// <param name="errString"> </param>
/// <param name="user"> </param>
public static void WriteLog(string errString, string user,string url)
{
string fileName = DateTime.Now.ToShortDateString() + ".log";
StreamWriter sr;
string path = LogPath + fileName;
if (File.Exists(path))
{
sr = File.AppendText(path);
sr.Write("错误源: ");
sr.Write(url);
sr.Write("错误信息: ");
sr.WriteLine(errString);
sr.Write("编写者: ");
sr.WriteLine(user);
sr.Write("时间: ");
sr.WriteLine(DateTime.Now);
sr.WriteLine("---------------------------------------------------");
}
else //如果文件不存在
{
DirectoryInfo di = new DirectoryInfo(LogPath);
di.Create();
sr = new StreamWriter(path, true);
sr.Write("错误源:");
sr.WriteLine(url);
sr.Write("错误信息: ");
sr.WriteLine(errString);
sr.Write("编写者: ");
sr.WriteLine(user);
sr.Write("时间: ");
sr.WriteLine(DateTime.Now);
sr.WriteLine("---------------------------------------------------");
}
sr.Close();
}
}
二
public void Log(Exception ex)
{
FileInfo file = new FileInfo(System.Windows.Forms.Application.StartupPath + "//ErrorLog.txt");
if (!file.Exists)
{
file.Create();
}
StreamWriter stream = file.AppendText();
stream.WriteLine("DateTime:" + DateTime.Now.ToString());
stream.WriteLine(ex.Message);
stream.Close();
}