⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.cs

📁 一个很好的网上购物系统!进行了新的修改具有很多的功能!
💻 CS
字号:
using System;
using System.Data;
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 Commerce.Providers;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Log
/// </summary>
public class Log {
    public static void Write(string sEvent,string category,string message, string itemID,string userName){
        WriteLog(category, sEvent, message, itemID, userName);
    }
    public static void Write(string sEvent, string category, string message, string userName) {
        WriteLog(category, sEvent, message, "", userName);
  
    }
    public static void Write(string sEvent, string message, string userName) {
        WriteLog("一般", sEvent, message, "", userName);

    }
    public static void Write(string message, string userName) {
        WriteLog("一般", "信息", message, "", userName);

    }
    public static void Write(string message) {
        WriteLog("一般", "信息", message, "", "系统");

    }
    public static void Write(Exception x,string userName) {
        WriteLog("错误", "程序错误", x.Message+"\r\n"+GetStackTrace(x), "", "系统");

    }
    static void WriteLog(string sEvent, string category, string message, string itemID, string userName) {
       try {
           LogInsert(category, sEvent, message,itemID, userName);
       } catch{
       }
    }
    #region DB Call
    public static void LogInsert(string category, string sEvent, string message, string itemID, string userName) {
        //Define the parameters
        SqlParameter[] paramArray = new SqlParameter[]
					{
						new SqlParameter("@category", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Current, category),
						new SqlParameter("@event", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Current, sEvent),
						new SqlParameter("@itemID", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Current, itemID),
						new SqlParameter("@userName", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Current, userName),
						new SqlParameter("@message", SqlDbType.VarChar, 1000, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Current, message),
					};
        try {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CommerceTemplate"].ConnectionString;
            //Execute the command using the connection string from the db base class 
            //and get the number of rows affected by the operation
            Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(connString, CommandType.StoredProcedure, "CMRC_LOGS_Insert", paramArray);
        } catch (SqlException x) {
            HandleError(paramArray, x, "CMRC_LOGS_Insert");

        }
    }
    #endregion

    #region Error Handling
    static void HandleError(SqlParameter[] paramArray, SqlException x, string sprocName) {
        string sException = "错误执行 " + sprocName + ": " + x.Message + " \r\n";
        foreach (SqlParameter p in paramArray) {
            sException += p.ParameterName + "=" + p.Value + "\r\n";
        }
        throw new Exception(sException, x);
    }

    #endregion
    static string GetStackTrace(Exception x) {

        Exception logException = x;
        if (x.InnerException != null)
            logException = x.InnerException;

        string strErrorMsg = "\n\n错误路径 :" + System.Web.HttpContext.Current.Request.Path;

        // Get the QueryString along with the Virtual Path
        strErrorMsg += "\n\n错误虚拟路径 :" + System.Web.HttpContext.Current.Request.RawUrl;


        // Get the error message
        strErrorMsg += "\n\n错误信息 :" + logException.Message;

        // Source of the message
        strErrorMsg += "\n\n错误源 :" + logException.Source;

        // Stack Trace of the error

        strErrorMsg += "\n\n错误堆栈 :" + logException.StackTrace;

        // Method where the error occurred
        strErrorMsg += "\n\n错误目标 :" + logException.TargetSite;
        return strErrorMsg;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -