c# 日志异常类

  1. using System;   
  2. using System.Xml;   
  3. using System.Data.SqlClient;   
  4. using System.Collections;   
  5. using System.Data;   
  6. using System.Web;   
  7. using System.IO;   
  8. using System.Web.UI;   
  9.   
  10. namespace Auralog.Elisa.Logs   
  11. {   
  12.     public static class Log   
  13.     {   
  14.         Private Fields#region Private Fields   
  15.         private static  XmlDocument xmlDocument = new XmlDocument();   
  16.         private static string xmlFileName = "";   
  17.         const string errorPath = "~/Error.aspx";  
  18.         #endregion   
  19.   
  20.         Private Methods#region Private Methods   
  21.   
  22.         //if have Exception Redirect error page   
  23.         private static void JumpToErrorPage()   
  24.         {   
  25.             HttpContext.Current.Response.Redirect(errorPath, true);   
  26.         }   
  27.   
  28.         private static string FormatInt(int num)   
  29.         {   
  30.             string formattedString = "";   
  31.             if (num <= 9)   
  32.             {   
  33.                 formattedString = "0" + num.ToString();   
  34.             }   
  35.             else  
  36.             {   
  37.                 formattedString = num.ToString();   
  38.             }   
  39.   
  40.             return formattedString;   
  41.         }   
  42.   
  43.         private static string GetFileName()   
  44.         {   
  45.             string year = DateTime.Now.Year.ToString();   
  46.             string month = FormatInt(DateTime.Now.Month);   
  47.             string day = FormatInt(DateTime.Now.Day);   
  48.             //string path = "web/Log/";   
  49.             string path = System.Web.HttpContext.Current.Server.MapPath("~/log/");   
  50.   
  51.             string file = path + "Log" + year + month + day + ".xml";   
  52.                
  53.             return file;   
  54.         }   
  55.   
  56.         /**//// <SUMMARY>   
  57.         /// create log file path   
  58.         /// </SUMMARY>   
  59.         private static void CreateLogFile()   
  60.         {   
  61.             string xmlRootElementStr = "<?xml version=/"1.0/" encoding=/"utf-8/"?>";   
  62.             xmlRootElementStr += "<LOG>";   
  63.             xmlRootElementStr += "    <SYSTEM>";   
  64.             xmlRootElementStr += "        <EXCEPTIONS>";   
  65.             xmlRootElementStr += "        </EXCEPTIONS>";   
  66.             xmlRootElementStr += "    </SYSTEM>";   
  67.             xmlRootElementStr += "    <SQL>";   
  68.             xmlRootElementStr += "        <EXCEPTIONS>";   
  69.             xmlRootElementStr += "        </EXCEPTIONS>";   
  70.             xmlRootElementStr += "    </SQL>";   
  71.             xmlRootElementStr += "</LOG>";   
  72.   
  73.             xmlFileName = GetFileName();   
  74.             try  
  75.             {   
  76.                 //if file inexistence,create a new log file and load this file   
  77.                 if (!System.IO.File.Exists(xmlFileName))   
  78.                 {   
  79.                     byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);   
  80.                     System.IO.FileStream fileStream = new System.IO.FileStream(xmlFileName, FileMode.Create);   
  81.                     fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);   
  82.                     fileStream.Close();   
  83.                     xmlDocument.Load(xmlFileName);   
  84.                 }   
  85.                 else  
  86.                 {   
  87.                     xmlDocument.Load(xmlFileName);   
  88.                 }   
  89.             }   
  90.             catch (Exception ex)   
  91.             {   
  92.                 throw ex;   
  93.             }   
  94.         }  
  95.         #endregion   
  96.   
  97.         Public Methods#region Public Methods   
  98.   
  99.         /**//// <SUMMARY>   
  100.         /// record a system exception to log   
  101.         /// </SUMMARY>   
  102.         /// <PARAM name="ex"></PARAM>   
  103.         public static void AddSystemException(Exception ex)   
  104.         {   
  105.             //Get    
  106.             CreateLogFile();   
  107.   
  108.             XmlNode xmlNode = xmlDocument.SelectSingleNode("//System//exceptions");   
  109.   
  110.             XmlElement exceptionNode = xmlDocument.CreateElement("exception");   
  111.   
  112.             XmlElement timeNode = xmlDocument.CreateElement("Time");   
  113.   
  114.             XmlElement messageNode = xmlDocument.CreateElement("Message");   
  115.             XmlElement sourceNode = xmlDocument.CreateElement("Source");   
  116.             XmlElement stacktraceNode = xmlDocument.CreateElement("StackTrace");   
  117.   
  118.   
  119.             DateTime dateTime = DateTime.Now;   
  120.             string messageString = ex.Message;   
  121.             string sourceString = ex.Source;   
  122.             string stacktrace = ex.StackTrace;   
  123.   
  124.             timeNode.InnerText = dateTime.ToString();   
  125.   
  126.             messageNode.InnerText = messageString;   
  127.             sourceNode.InnerText = sourceString;   
  128.             stacktraceNode.InnerText = stacktrace;   
  129.   
  130.             exceptionNode.AppendChild(timeNode);   
  131.   
  132.             exceptionNode.AppendChild(messageNode);   
  133.             exceptionNode.AppendChild(sourceNode);   
  134.             exceptionNode.AppendChild(stacktraceNode);   
  135.   
  136.             xmlNode.AppendChild(exceptionNode);   
  137.   
  138.             xmlDocument.Save(xmlFileName);   
  139.             JumpToErrorPage();   
  140.         }   
  141.   
  142.         // record a sql exception to log   
  143.         public static void AddSqlException(Exception ex)   
  144.         {   
  145.             //Get   
  146.             CreateLogFile();   
  147.   
  148.             XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//exceptions");   
  149.   
  150.             XmlElement exceptionNode = xmlDocument.CreateElement("exception");   
  151.   
  152.             XmlElement timeNode = xmlDocument.CreateElement("Time");   
  153.   
  154.             XmlElement messageNode = xmlDocument.CreateElement("Message");   
  155.             XmlElement sourceNode = xmlDocument.CreateElement("Source");   
  156.             XmlElement stacktraceNode = xmlDocument.CreateElement("StackTrace");   
  157.   
  158.   
  159.             DateTime dateTime = DateTime.Now;   
  160.             string messageString = ex.Message;   
  161.             string sourceString = ex.Source;   
  162.             string stacktrace = ex.StackTrace;   
  163.   
  164.             timeNode.InnerText = dateTime.ToString();   
  165.   
  166.             messageNode.InnerText = messageString;   
  167.             sourceNode.InnerText = sourceString;   
  168.             stacktraceNode.InnerText = stacktrace;   
  169.   
  170.             exceptionNode.AppendChild(timeNode);   
  171.   
  172.             exceptionNode.AppendChild(messageNode);   
  173.             exceptionNode.AppendChild(sourceNode);   
  174.             exceptionNode.AppendChild(stacktraceNode);   
  175.   
  176.             xmlNode.AppendChild(exceptionNode);   
  177.   
  178.             xmlDocument.Save(xmlFileName);   
  179.             JumpToErrorPage();   
  180.         }  
  181.         #endregion   
  182.     }   
  183. }<PRE></PRE>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值