您的位置:首页 > 编程语言 > ASP

Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)

2012-02-14 10:03 1301 查看
using System.Web.Security;
using System.Web.SessionState;
using System.Data;

protected void Application_Error(object sender, EventArgs e)
{
//捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(详细说明)。 - 2012-02-13
HttpContext ctx = HttpContext.Current;
if (ctx == null) return;
try
{
Exception erroy = Server.GetLastError();
string LogErr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ";出错页面:" + Request.Url.ToString() + ";访问IP:" + Request.UserHostAddress.ToString() + "\r\n";
LogErr += "异常信息:(" + erroy.Message + ")\r\n";
LogErr += "异常方法:(" + erroy.TargetSite + ")\r\n";
LogErr += "异常来源:(" + erroy.Source + ")\r\n";
LogErr += "异常处理:\r\n" + erroy.StackTrace + "\r\n";
LogErr += "异常实例:\r\n" + erroy.InnerException + "\r\n";
LogErr += "//**********************************************************************************************************************" + "\r\n";

Platform.Controllers.P_LogInfo.WriteTextLog(LogErr);
//**********************************************************************************************************************
////Window系统安全日志
//Platform.Controllers.P_LogInfo.WriteTextLog(LogErr);
////文本文件安全日志,带类命名空间
//Platform.Controllers.P_LogInfo.WriteTextLog("Platform.B2C", LogErr);
////文本文件安全日志
//Platform.Controllers.P_LogInfo.WriteWindowLog(LogErr);
////数据库SQL安全日志
//Platform.Controllers.P_LogInfo.WriteSQLLog("网站登陆", Platform.Controllers.P_LogInfo.LogErrType.B2CLog, LogErr, HttpContext.Current.User);
//**********************************************************************************************************************
}
catch
{
}
finally
{
//清除前一个异常
Server.ClearError();

//此处不是page中,不能用Response.Redirect("../frmSysError.aspx");
//System.Web.HttpContext.Current.Response.Redirect("http://" + HttpContext.Current.Request.Url.Host.ToString() + "/UpdateTip.htm");
}
}


日志类...:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Security.Principal;
using System.Reflection;
using System.Diagnostics;
/*
* 功能描述:网站日志
* 创建人:  **
* 创建日期:2012年02月13日
*/
namespace Platform
{
/// <summary>
/// 同行网站日志
/// </summary>
public class LogInfo
{
public LogInfo()
{
}

/// <summary>
/// 1)Window系统安全日志: 写入日志到window系统日志(事件查看器中查看:)
/// </summary>
/// <param name="strMessage">日志详细</param>
/// <returns></returns>
public static void WriteWindowLog(string strMessage)
{
EventLog eventLog = null;
string sourceName = "Platform";
//确定日志是否存在
if (!(EventLog.SourceExists(sourceName)))
{
EventLog.CreateEventSource(sourceName, sourceName + "Log");
}
if (eventLog == null)
{
eventLog = new EventLog(sourceName + "Log");
eventLog.Source = sourceName;
}
//记录日志安全信息
eventLog.WriteEntry(strMessage, System.Diagnostics.EventLogEntryType.Error);
}
/// <summary>
/// 2.1)文本文件安全日志: 写入日志到文本文件,并指定写入类的命名空间
/// </summary>
/// <param name="nameSpace">命名空间</param>
/// <param name="strMessage">日志详细</param>
public static void WriteTextLog(string nameSpace, string strMessage)
{
strMessage = DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString() + ", NameSpace:" + nameSpace + "\r\n" + "ErrorMessage:" + strMessage;
string path = AppDomain.CurrentDomain.BaseDirectory + @"TBSystem\Log\";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string fileFullPath = path + DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + ".TBSystem.Shop.txt";
StreamWriter sw;
if (!File.Exists(fileFullPath))
{
sw = File.CreateText(fileFullPath);
}
else
{
sw = File.AppendText(fileFullPath);
}
sw.WriteLine(strMessage);
sw.Close();
}
/// <summary>
///  2.2)文本文件安全日志: 写入日志到文本文件
/// </summary>
/// <param name="strMessage">日志详细(无命名空间)</param>
public static void WriteTextLog(string strMessage)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"TBSystem\Log\";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);

string fileFullPath = path + DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Day.ToString() + ".TBSystem.Shop.txt";
StreamWriter sw;
if (!File.Exists(fileFullPath))
{
sw = File.CreateText(fileFullPath);
}
else
{
sw = File.AppendText(fileFullPath);
}
sw.WriteLine(strMessage);
sw.Close();
}
/// <summary>
///3)数据库日志: 记录操作安全日志,并写入数据库
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="logType">日志类型</param>
/// <param name="strMessage">日志详细</param>
/// <param name="user">操作人</param>
/// <returns></returns>
public static bool WriteSQLLog(string logName, LogErrType logType, string strMessage, IPrincipal user)
{
try
{
//从登陆用户中得到帐号
string account = "";  //GetAccountFromLogin(user);
//string sql = "Produce_AddLog ";
SqlParameter[] sp = new SqlParameter[4];
sp[0] = new SqlParameter("@Account", SqlDbType.NVarChar);
sp[0].Value = account;
sp[1] = new SqlParameter("@LogName", SqlDbType.NVarChar);
sp[1].Value = logName;
sp[2] = new SqlParameter("@LogModule", SqlDbType.Int);
sp[2].Value = (int)logType;
sp[3] = new SqlParameter("@LogExplain", SqlDbType.NVarChar);
sp[3].Value = strMessage;
//new Database(DatabaseName.produceDB).ExecuteNonQuery(sql, sp, true);
return true;
}
catch (Exception ex)
{
WriteTextLog("TBSystem.Shop.Log", "写" + logName + "操作日志错误:" + ex.Message);
return false;
}
}
}
/// <summary>
/// 日志类型
/// </summary>
public enum LogErrType
{
SystemLog,
B2BLog,
B2CLog
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐