c# 日志异常类

using System;
using System.Xml;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.Web;
using System.IO;
using System.Web.UI;

namespace Auralog.Elisa.Logs
{
    public static class Log
    {
        Private Fields#region Private Fields
        private static  XmlDocument xmlDocument = new XmlDocument();
        private static string xmlFileName = "";
        const string errorPath = "~/Error.aspx";
        #endregion

        Private Methods#region Private Methods

        //if have Exception Redirect error page
        private static void JumpToErrorPage()
        {
            HttpContext.Current.Response.Redirect(errorPath, true);
        }

        private static string FormatInt(int num)
        {
            string formattedString = "";
            if (num <= 9)
            {
                formattedString = "0" + num.ToString();
            }
            else
            {
                formattedString = num.ToString();
            }

            return formattedString;
        }

        private static string GetFileName()
        {
            string year = DateTime.Now.Year.ToString();
            string month = FormatInt(DateTime.Now.Month);
            string day = FormatInt(DateTime.Now.Day);
            //string path = "web/Log/";
            string path = System.Web.HttpContext.Current.Server.MapPath("~/log/");

            string file = path + "Log" + year + month + day + ".xml";
            
            return file;
        }

        /** 
        /// create log file path
        /// 
        private static void CreateLogFile()
        {
            string xmlRootElementStr = "
 
 ";
            xmlRootElementStr += "
 
 
  
  ";
            xmlRootElementStr += "    
  
  
   
   ";
            xmlRootElementStr += "        
   
   
    
    ";
            xmlRootElementStr += "        
   
   ";
            xmlRootElementStr += "    
  
  ";
            xmlRootElementStr += "    
  
  
   
   ";
            xmlRootElementStr += "        
   
   
    
    ";
            xmlRootElementStr += "        
   
   ";
            xmlRootElementStr += "    
  
  ";
            xmlRootElementStr += "
 
 ";

            xmlFileName = GetFileName();
            try
            {
                //if file inexistence,create a new log file and load this file
                if (!System.IO.File.Exists(xmlFileName))
                {
                    byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);
                    System.IO.FileStream fileStream = new System.IO.FileStream(xmlFileName, FileMode.Create);
                    fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);
                    fileStream.Close();
                    xmlDocument.Load(xmlFileName);
                }
                else
                {
                    xmlDocument.Load(xmlFileName);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        Public Methods#region Public Methods

        /** 
        /// record a system exception to log
        /// 
        /// 
        public static void AddSystemException(Exception ex)
        {
            //Get 
            CreateLogFile();

            XmlNode xmlNode = xmlDocument.SelectSingleNode("//System//exceptions");

            XmlElement exceptionNode = xmlDocument.CreateElement("exception");

            XmlElement timeNode = xmlDocument.CreateElement("Time");

            XmlElement messageNode = xmlDocument.CreateElement("Message");
            XmlElement sourceNode = xmlDocument.CreateElement("Source");
            XmlElement stacktraceNode = xmlDocument.CreateElement("StackTrace");


            DateTime dateTime = DateTime.Now;
            string messageString = ex.Message;
            string sourceString = ex.Source;
            string stacktrace = ex.StackTrace;

            timeNode.InnerText = dateTime.ToString();

            messageNode.InnerText = messageString;
            sourceNode.InnerText = sourceString;
            stacktraceNode.InnerText = stacktrace;

            exceptionNode.AppendChild(timeNode);

            exceptionNode.AppendChild(messageNode);
            exceptionNode.AppendChild(sourceNode);
            exceptionNode.AppendChild(stacktraceNode);

            xmlNode.AppendChild(exceptionNode);

            xmlDocument.Save(xmlFileName);
            JumpToErrorPage();
        }

        // record a sql exception to log
        public static void AddSqlException(Exception ex)
        {
            //Get
            CreateLogFile();

            XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//exceptions");

            XmlElement exceptionNode = xmlDocument.CreateElement("exception");

            XmlElement timeNode = xmlDocument.CreateElement("Time");

            XmlElement messageNode = xmlDocument.CreateElement("Message");
            XmlElement sourceNode = xmlDocument.CreateElement("Source");
            XmlElement stacktraceNode = xmlDocument.CreateElement("StackTrace");


            DateTime dateTime = DateTime.Now;
            string messageString = ex.Message;
            string sourceString = ex.Source;
            string stacktrace = ex.StackTrace;

            timeNode.InnerText = dateTime.ToString();

            messageNode.InnerText = messageString;
            sourceNode.InnerText = sourceString;
            stacktraceNode.InnerText = stacktrace;

            exceptionNode.AppendChild(timeNode);

            exceptionNode.AppendChild(messageNode);
            exceptionNode.AppendChild(sourceNode);
            exceptionNode.AppendChild(stacktraceNode);

            xmlNode.AppendChild(exceptionNode);

            xmlDocument.Save(xmlFileName);
            JumpToErrorPage();
        }
        #endregion
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值