• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
智美手机APP
国内最专业的智能手机APP应用解决方案提供商。智美手机APP为您提供成熟稳定的专业化产品、产品定制等服务。
博客园    首页    新随笔    联系   管理    订阅  订阅

异常处理 日志类

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


        
/**//// <summary>
        
/// create log file path
        
/// </summary>

        private static void CreateLogFile()
        
{
            
string xmlRootElementStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            xmlRootElementStr 
+= "<log>";
            xmlRootElementStr 
+= "    <System>";
            xmlRootElementStr 
+= "        <exceptions>";
            xmlRootElementStr 
+= "        </exceptions>";
            xmlRootElementStr 
+= "    </System>";
            xmlRootElementStr 
+= "    <Sql>";
            xmlRootElementStr 
+= "        <exceptions>";
            xmlRootElementStr 
+= "        </exceptions>";
            xmlRootElementStr 
+= "    </Sql>";
            xmlRootElementStr 
+= "</log>";

            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

        
/**//// <summary>
        
/// record a system exception to log
        
/// </summary>
        
/// <param name="ex"></param>

        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

    }

}

posted @ 2012-09-03 20:45  小敏博客园  阅读(485)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3