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

    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
    }
    }



  • 相关阅读:
    Mybatis—动态sql拼接问题
    小白用Mac
    JSP总结(jsp/EL表达式/核心标签)
    Spring定时任务配置
    通过简单示例来理解什么是机器学习
    在jupyter notebook中同时安装python2和python3
    Python读取和处理文件后缀为".sqlite"的数据文件
    Python:Anaconda安装虚拟环境到指定路径
    TIOBE:全球编程语言最新排名(Kotlin排名进入前50名)
    Python:一篇文章掌握Numpy的基本用法
  • 原文地址:https://www.cnblogs.com/Fooo/p/2350467.html
Copyright © 2011-2022 走看看