网上见过这样的文章,只是觉得做的不是太完善,于是就重新写了一个。
关于生成日志,其实已经有一个日志记录组件[Log4net],但里边的太多的东西没有用处,白白的浪费掉了不少的内存。
using
System;
using System.Xml;
using System.Web;
using System.Collections.Specialized;
using System.IO;
using System.Security;
using System.Threading;
using System.Security.Principal;
namespace Labour.Log
... {
/**//// <summary>
/// 生成异常日志类
/// </summary>
/// <remarks>
/// 根据日期,在指定xml文件中写入异常记录
/// </remarks>
public class ExceptionManager
...{
public ExceptionManager()
...{
}
private static string EXCEPTIONMANAGER_NAME = typeof(ExceptionManager).Name;
private static string EXCEPTIONLOG_DIRECTORY = "Exception Log";
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 异常处理方法
/// '' </summary>
/// '' <param name="exception"> 传入异常</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
public static void Publish(Exception exception)
...{
Publish(exception, System.Windows.Forms.Application.ProductName);
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 异常处理方法
/// '' </summary>
/// ''<param name="exception">传入异常</param>
/// '' <param name="exception"> 传入要显示的title</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
public static void Publish(Exception exception, string caption)
...{
try
...{
// 写入log
writeExceptionToLogFile(exception, caption);
}
catch (System.Exception )
...{
}
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 写log方法
/// '' </summary>
/// '' <returns>传入异常</returns>
/// ''<param name="caption">传入title</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
//private static void writeExceptionToLogFile()
//{
//}
private static void writeExceptionToLogFile(Exception exception, string caption)
...{
// 定义log文件路径
string logPath = getLogPath();
// 定义exception XML,调用SerializeToXML方法,把本次exception的信息组织起来,然会返回给newDoc
XmlDocument newDoc = SerializeToXML(exception, CreateAdditionalInfo(caption));
// 定义要输出到xml的文本
string outString;
//定义log里也有的历史记录
XmlDocument oldDoc = new XmlDocument();
// 如果已有log文件,则读取内容
if (File.Exists(logPath))
...{
oldDoc.Load(logPath);
}
// 把root节点下的内容读取出来
string oldExceptionContent="0";
if ((!(oldDoc == null)
&& (!(oldDoc.ChildNodes == null)
&& (oldDoc.ChildNodes.Count == 1))))
...{
oldExceptionContent = oldDoc.ChildNodes[0].InnerXml;
}
// 要输出到xml的文本
outString = ("<root>"
+ (oldExceptionContent
+ (newDoc.InnerXml + "</root>")));
FileStream fs = File.Open(logPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//写入文件
StreamWriter writer = new StreamWriter(fs);
writer.Write(outString);
writer.Close();
fs.Close();
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 获取log文件路径
/// '' </summary>
/// '' <returns>返回log文件具体路径</returns>
/// '' <remarks>
/// '' 返回值类似于:Exception Log6-02-26-log.xml
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
//private void getLogPath() ;
static string getLogPath()
...{
// 根据日期得到文件组成部分
string file_prfix = DateTime.Now.ToString("yyyy-MM-dd");
// 找存放log文件的目录,无则创建
if (!Directory.Exists(EXCEPTIONLOG_DIRECTORY))
...{
Directory.CreateDirectory(EXCEPTIONLOG_DIRECTORY);
}
// 返回
return (EXCEPTIONLOG_DIRECTORY + ("/"
+ (file_prfix + "-log.xml")));
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
///'' 获取附加信息
/// '' </summary>
/// '' <param name="caption">title</param>
/// '' <returns>返回一些AdditionalInfo的集合</returns>
/// '' <remarks>
/// '' 该集合主要是当前电脑的一些信息,比如操作系统等
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
static NameValueCollection CreateAdditionalInfo(string caption)
...{
NameValueCollection additionalInfo = new NameValueCollection();
additionalInfo.Add((EXCEPTIONMANAGER_NAME + ".Title"), caption);
return additionalInfo;
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// ''组织xml方法
/// '' </summary>
/// '' <param name="exception">传入异常</param>
/// '' <param name="additionalInfo">传入附加信息集合</param>
/// '' <returns></returns>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
static XmlDocument SerializeToXML(Exception exception, NameValueCollection additionalInfo)
...{
XmlDocument xmlDoc = new XmlDocument();
try
...{
// 定义1个根节点,2个子节点的名称
string xmlNodeName_ROOT = "ExceptionInformation";
string xmlNodeName_ADDITIONAL_INFORMATION = "AdditionalInformationProperty";
string xmlNodeName_EXCEPTION = "Exception";
// 定义一个xml文档
XmlElement xmlRoot = xmlDoc.CreateElement(xmlNodeName_ROOT);
xmlDoc.AppendChild(xmlRoot);
// 写入根节点 写入2个子节点
XmlElement xmlChild1 = xmlDoc.CreateElement(xmlNodeName_ADDITIONAL_INFORMATION);
XmlElement xmlChild2 = xmlDoc.CreateElement(xmlNodeName_EXCEPTION);
xmlRoot.AppendChild(xmlChild2);
//定义节点下的元素
XmlElement element;
// 元素值
XmlText elementText;
// 把附加信息集合的所有项目,都加入的子节点"AdditionalInformationProperty"下.
if ((!(additionalInfo == null)
&& (additionalInfo.Count > 0)))
...{
foreach (string i in additionalInfo)
...{
element = xmlDoc.CreateElement(i.Replace(" ", "_"));
elementText = xmlDoc.CreateTextNode(additionalInfo.Get(i));
element.AppendChild(elementText);
xmlChild1.AppendChild(element);
}
}
// 把异常的一些详细信息都加入到子节点"Exception"下
if (!(exception == null))
...{
//加入全球唯一标示符"Guid"元素
element = xmlDoc.CreateElement("Guid");
elementText = xmlDoc.CreateTextNode(System.Guid.NewGuid().ToString());
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
// 加入"ExceptionType"元素
element = xmlDoc.CreateElement("ExceptionType");
elementText = xmlDoc.CreateTextNode(exception.GetType().FullName);
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
//加入"Guid" " Message","TargetSite","Source","StackTrace"4个元素
foreach (System.Reflection.PropertyInfo p in exception.GetType().GetProperties())
...{
if (((p.Name == "Guid")
||((p.Name == "Message")
|| ((p.Name == "TargetSite")
|| ((p.Name == "Source")
|| (p.Name == "StackTrace"))))))
...{
if (!(p.GetValue(exception, null) == null))
...{
element = xmlDoc.CreateElement(p.Name);
elementText = xmlDoc.CreateTextNode(p.GetValue(exception, null).ToString());
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
}
}
}
}
// 返回
return xmlDoc;
}
//catch (Exception e)
catch (Exception)
...{
return xmlDoc;
}
}
}
}
using System.Xml;
using System.Web;
using System.Collections.Specialized;
using System.IO;
using System.Security;
using System.Threading;
using System.Security.Principal;
namespace Labour.Log
... {
/**//// <summary>
/// 生成异常日志类
/// </summary>
/// <remarks>
/// 根据日期,在指定xml文件中写入异常记录
/// </remarks>
public class ExceptionManager
...{
public ExceptionManager()
...{
}
private static string EXCEPTIONMANAGER_NAME = typeof(ExceptionManager).Name;
private static string EXCEPTIONLOG_DIRECTORY = "Exception Log";
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 异常处理方法
/// '' </summary>
/// '' <param name="exception"> 传入异常</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
public static void Publish(Exception exception)
...{
Publish(exception, System.Windows.Forms.Application.ProductName);
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 异常处理方法
/// '' </summary>
/// ''<param name="exception">传入异常</param>
/// '' <param name="exception"> 传入要显示的title</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
public static void Publish(Exception exception, string caption)
...{
try
...{
// 写入log
writeExceptionToLogFile(exception, caption);
}
catch (System.Exception )
...{
}
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 写log方法
/// '' </summary>
/// '' <returns>传入异常</returns>
/// ''<param name="caption">传入title</param>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
//private static void writeExceptionToLogFile()
//{
//}
private static void writeExceptionToLogFile(Exception exception, string caption)
...{
// 定义log文件路径
string logPath = getLogPath();
// 定义exception XML,调用SerializeToXML方法,把本次exception的信息组织起来,然会返回给newDoc
XmlDocument newDoc = SerializeToXML(exception, CreateAdditionalInfo(caption));
// 定义要输出到xml的文本
string outString;
//定义log里也有的历史记录
XmlDocument oldDoc = new XmlDocument();
// 如果已有log文件,则读取内容
if (File.Exists(logPath))
...{
oldDoc.Load(logPath);
}
// 把root节点下的内容读取出来
string oldExceptionContent="0";
if ((!(oldDoc == null)
&& (!(oldDoc.ChildNodes == null)
&& (oldDoc.ChildNodes.Count == 1))))
...{
oldExceptionContent = oldDoc.ChildNodes[0].InnerXml;
}
// 要输出到xml的文本
outString = ("<root>"
+ (oldExceptionContent
+ (newDoc.InnerXml + "</root>")));
FileStream fs = File.Open(logPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//写入文件
StreamWriter writer = new StreamWriter(fs);
writer.Write(outString);
writer.Close();
fs.Close();
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// '' 获取log文件路径
/// '' </summary>
/// '' <returns>返回log文件具体路径</returns>
/// '' <remarks>
/// '' 返回值类似于:Exception Log6-02-26-log.xml
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
//private void getLogPath() ;
static string getLogPath()
...{
// 根据日期得到文件组成部分
string file_prfix = DateTime.Now.ToString("yyyy-MM-dd");
// 找存放log文件的目录,无则创建
if (!Directory.Exists(EXCEPTIONLOG_DIRECTORY))
...{
Directory.CreateDirectory(EXCEPTIONLOG_DIRECTORY);
}
// 返回
return (EXCEPTIONLOG_DIRECTORY + ("/"
+ (file_prfix + "-log.xml")));
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
///'' 获取附加信息
/// '' </summary>
/// '' <param name="caption">title</param>
/// '' <returns>返回一些AdditionalInfo的集合</returns>
/// '' <remarks>
/// '' 该集合主要是当前电脑的一些信息,比如操作系统等
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
static NameValueCollection CreateAdditionalInfo(string caption)
...{
NameValueCollection additionalInfo = new NameValueCollection();
additionalInfo.Add((EXCEPTIONMANAGER_NAME + ".Title"), caption);
return additionalInfo;
}
/**//// '' -----------------------------------------------------------------------------
/// '' <summary>
/// ''组织xml方法
/// '' </summary>
/// '' <param name="exception">传入异常</param>
/// '' <param name="additionalInfo">传入附加信息集合</param>
/// '' <returns></returns>
/// '' <remarks>
/// '' </remarks>
/// '' <history>
/// '' [Gong] 2007-05-08 Created
/// '' </history>
/// '' -----------------------------------------------------------------------------
static XmlDocument SerializeToXML(Exception exception, NameValueCollection additionalInfo)
...{
XmlDocument xmlDoc = new XmlDocument();
try
...{
// 定义1个根节点,2个子节点的名称
string xmlNodeName_ROOT = "ExceptionInformation";
string xmlNodeName_ADDITIONAL_INFORMATION = "AdditionalInformationProperty";
string xmlNodeName_EXCEPTION = "Exception";
// 定义一个xml文档
XmlElement xmlRoot = xmlDoc.CreateElement(xmlNodeName_ROOT);
xmlDoc.AppendChild(xmlRoot);
// 写入根节点 写入2个子节点
XmlElement xmlChild1 = xmlDoc.CreateElement(xmlNodeName_ADDITIONAL_INFORMATION);
XmlElement xmlChild2 = xmlDoc.CreateElement(xmlNodeName_EXCEPTION);
xmlRoot.AppendChild(xmlChild2);
//定义节点下的元素
XmlElement element;
// 元素值
XmlText elementText;
// 把附加信息集合的所有项目,都加入的子节点"AdditionalInformationProperty"下.
if ((!(additionalInfo == null)
&& (additionalInfo.Count > 0)))
...{
foreach (string i in additionalInfo)
...{
element = xmlDoc.CreateElement(i.Replace(" ", "_"));
elementText = xmlDoc.CreateTextNode(additionalInfo.Get(i));
element.AppendChild(elementText);
xmlChild1.AppendChild(element);
}
}
// 把异常的一些详细信息都加入到子节点"Exception"下
if (!(exception == null))
...{
//加入全球唯一标示符"Guid"元素
element = xmlDoc.CreateElement("Guid");
elementText = xmlDoc.CreateTextNode(System.Guid.NewGuid().ToString());
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
// 加入"ExceptionType"元素
element = xmlDoc.CreateElement("ExceptionType");
elementText = xmlDoc.CreateTextNode(exception.GetType().FullName);
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
//加入"Guid" " Message","TargetSite","Source","StackTrace"4个元素
foreach (System.Reflection.PropertyInfo p in exception.GetType().GetProperties())
...{
if (((p.Name == "Guid")
||((p.Name == "Message")
|| ((p.Name == "TargetSite")
|| ((p.Name == "Source")
|| (p.Name == "StackTrace"))))))
...{
if (!(p.GetValue(exception, null) == null))
...{
element = xmlDoc.CreateElement(p.Name);
elementText = xmlDoc.CreateTextNode(p.GetValue(exception, null).ToString());
element.AppendChild(elementText);
xmlChild2.AppendChild(element);
}
}
}
}
// 返回
return xmlDoc;
}
//catch (Exception e)
catch (Exception)
...{
return xmlDoc;
}
}
}
}