Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
2012-02-14 10:03
1906 查看
using System.Web.Security;
using System.Web.SessionState;
using System.Data;
日志类...:
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 } }
相关文章推荐
- ASP.NET全局错误处理和异常日志记录以及IIS配置自定义错误页面
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(2):处理异步调用中的异常
- Asp.Net 2.0 中错误处理的几种方法(发邮件及记录信息到系统事件中)
- asp.net办公自动化系统开发实例导航的安装出现错误的解决方法
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- C# B/S程序中出现“异常详细信息: System.Data.SqlClient.SqlException: 用户 'XXX/ASPNET' 登录失败。”的处理方法
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- [转载]针对IIS7以上的ASP.NET网站自定义错误页面与异常日志总结
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- 代码阅读总结之Fitch and Mather 7.0(asp.net发生异常或错误时错误提示页面的处理方法)
- Asp.Net中Web.Config中的数据库连接字符串的调用方法(初学者实例)
- asp.net记录错误日志的方法
- Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
- ASP.NET下的系统错误记录管理工具 ELMAH使用方法
- 网站访问地址是.shtml访问出现HTTP错误405.0处理方法
- 一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!
- [置顶] Ajax程序:处理异步调用中的异常(使用Asp.Net Ajax内建的异常处理方法)
- jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码