Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace Utility
{
public static class LogWriter
{
#region Private Fields
private static XmlDocument xmlDocument = new XmlDocument();
private static string xmlFileName = "";
#endregion
#region Private Methods
private static bool HasChild(string name, XmlNode node)
{
XmlNodeList xmlnodelist = node.ChildNodes;
foreach (XmlNode enode in xmlnodelist)
{
if (enode.Name.ToLower() == name.ToLower())
{
return true;
}
}
return false;
}
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 = "./Logs/";
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\"?>\r\n\r\n";
xmlRootElementStr += "<Log>\r\n\r\n";
xmlRootElementStr += " <Server>\r\n\r\n";
xmlRootElementStr += " <Infomations>\r\n\r\n";
xmlRootElementStr += " </Infomations>\r\n\r\n";
xmlRootElementStr += " </Server>\r\n\r\n";
xmlRootElementStr += " <System>\r\n";
xmlRootElementStr += " <Exceptions>\r\n";
xmlRootElementStr += " </Exceptions>\r\n";
xmlRootElementStr += " </System>\r\n\r\n";
xmlRootElementStr += " <Sql>\r\n";
xmlRootElementStr += " <Exceptions>\r\n";
xmlRootElementStr += " </Exceptions>\r\n";
xmlRootElementStr += " </Sql>\r\n\r\n";
xmlRootElementStr += "</Log>\r\n";
xmlFileName = GetFileName();
try
{
//if file inexistence,create a new log file and load this file
if (!System.IO.Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
// File.Create(xmlFileName);
byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);
System.IO.FileStream fileStream = new FileStream(xmlFileName, FileMode.Create, FileAccess.ReadWrite);
fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);
fileStream.Close();
xmlDocument.Load(xmlFileName);
}
else
{
if (!System.IO.File.Exists(xmlFileName))
{
// File.Create(xmlFileName);
byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);
System.IO.FileStream fileStream = new System.IO.FileStream(xmlFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);
fileStream.Close();
xmlDocument.Load(xmlFileName);
}
else
{
xmlDocument.Load(xmlFileName);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region Public Methods
/**/
/// <summary>
/// record a system exception to log
/// </summary>
/// <param name="ex"></param>
public static void AddSystemException(Exception ex)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//System//Exceptions");
if (!HasChild("exception", xmlNode))
{
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);
}
else
{
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);
}
catch (Exception e)
{
throw e;
}
}
// record a sql exception to log
public static void AddSqlException(Exception ex)
{
//Get
try
{
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//Exceptions");
if (!HasChild("exception", xmlNode))
{
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);
}
else
{
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);
}
catch (Exception e)
{
throw e;
}
}
// record a sql exception to log
public static void AddSqlException(Exception ex, string sqltxt)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//Exceptions");
if (!HasChild("exception", xmlNode))
{
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");
XmlElement sqltxtNode = xmlDocument.CreateElement("SQL_Text"); 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;
sqltxtNode.InnerText = sqltxt;
exceptionNode.AppendChild(timeNode);
exceptionNode.AppendChild(messageNode);
exceptionNode.AppendChild(sourceNode);
exceptionNode.AppendChild(stacktraceNode);
exceptionNode.AppendChild(sqltxtNode);
xmlNode.AppendChild(exceptionNode);
}
else
{
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");
XmlElement sqltxtNode = xmlDocument.CreateElement("SQL_Text");
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;
sqltxtNode.InnerText = sqltxt;
exceptionNode.AppendChild(timeNode);
exceptionNode.AppendChild(messageNode);
exceptionNode.AppendChild(sourceNode);
exceptionNode.AppendChild(stacktraceNode);
exceptionNode.AppendChild(sqltxtNode);
xmlNode.AppendChild(exceptionNode);
} xmlDocument.Save(xmlFileName);
}
catch (Exception e)
{
throw e;
}
}
public static void AddServerInfo(String msg, String src)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Server//Infomations");
if (!HasChild("Info", xmlNode))
{
XmlElement infoNode = xmlDocument.CreateElement("Info");
XmlElement timeNode = xmlDocument.CreateElement("Time");
XmlElement messageNode = xmlDocument.CreateElement("Message");
XmlElement sourceNode = xmlDocument.CreateElement("Source");
DateTime dateTime = DateTime.Now;
string messageString = msg;
string sourceString = src; timeNode.InnerText = dateTime.ToString();
messageNode.InnerText = messageString;
sourceNode.InnerText = sourceString; infoNode.AppendChild(timeNode);
infoNode.AppendChild(messageNode);
infoNode.AppendChild(sourceNode);
xmlNode.AppendChild(infoNode);
}
else
{
XmlElement infoNode = xmlDocument.CreateElement("Info");
XmlElement timeNode = xmlDocument.CreateElement("Time");
XmlElement messageNode = xmlDocument.CreateElement("Message");
XmlElement sourceNode = xmlDocument.CreateElement("Source");
DateTime dateTime = DateTime.Now;
string messageString = msg;
string sourceString = src;
timeNode.InnerText = dateTime.ToString();
messageNode.InnerText = messageString;
sourceNode.InnerText = sourceString; infoNode.AppendChild(timeNode);
infoNode.AppendChild(messageNode);
infoNode.AppendChild(sourceNode);
xmlNode.AppendChild(infoNode);
} xmlDocument.Save(xmlFileName);
}
catch (Exception e)
{
throw e;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace Utility
{
public static class LogWriter
{
#region Private Fields
private static XmlDocument xmlDocument = new XmlDocument();
private static string xmlFileName = "";
#endregion
#region Private Methods
private static bool HasChild(string name, XmlNode node)
{
XmlNodeList xmlnodelist = node.ChildNodes;
foreach (XmlNode enode in xmlnodelist)
{
if (enode.Name.ToLower() == name.ToLower())
{
return true;
}
}
return false;
}
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 = "./Logs/";
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\"?>\r\n\r\n";
xmlRootElementStr += "<Log>\r\n\r\n";
xmlRootElementStr += " <Server>\r\n\r\n";
xmlRootElementStr += " <Infomations>\r\n\r\n";
xmlRootElementStr += " </Infomations>\r\n\r\n";
xmlRootElementStr += " </Server>\r\n\r\n";
xmlRootElementStr += " <System>\r\n";
xmlRootElementStr += " <Exceptions>\r\n";
xmlRootElementStr += " </Exceptions>\r\n";
xmlRootElementStr += " </System>\r\n\r\n";
xmlRootElementStr += " <Sql>\r\n";
xmlRootElementStr += " <Exceptions>\r\n";
xmlRootElementStr += " </Exceptions>\r\n";
xmlRootElementStr += " </Sql>\r\n\r\n";
xmlRootElementStr += "</Log>\r\n";
xmlFileName = GetFileName();
try
{
//if file inexistence,create a new log file and load this file
if (!System.IO.Directory.Exists("Logs"))
{
Directory.CreateDirectory("Logs");
// File.Create(xmlFileName);
byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);
System.IO.FileStream fileStream = new FileStream(xmlFileName, FileMode.Create, FileAccess.ReadWrite);
fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);
fileStream.Close();
xmlDocument.Load(xmlFileName);
}
else
{
if (!System.IO.File.Exists(xmlFileName))
{
// File.Create(xmlFileName);
byte[] xmlRootElementByte = System.Text.Encoding.Default.GetBytes(xmlRootElementStr);
System.IO.FileStream fileStream = new System.IO.FileStream(xmlFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fileStream.Write(xmlRootElementByte, 0, xmlRootElementByte.Length);
fileStream.Close();
xmlDocument.Load(xmlFileName);
}
else
{
xmlDocument.Load(xmlFileName);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region Public Methods
/**/
/// <summary>
/// record a system exception to log
/// </summary>
/// <param name="ex"></param>
public static void AddSystemException(Exception ex)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//System//Exceptions");
if (!HasChild("exception", xmlNode))
{
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);
}
else
{
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);
}
catch (Exception e)
{
throw e;
}
}
// record a sql exception to log
public static void AddSqlException(Exception ex)
{
//Get
try
{
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//Exceptions");
if (!HasChild("exception", xmlNode))
{
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);
}
else
{
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);
}
catch (Exception e)
{
throw e;
}
}
// record a sql exception to log
public static void AddSqlException(Exception ex, string sqltxt)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Sql//Exceptions");
if (!HasChild("exception", xmlNode))
{
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");
XmlElement sqltxtNode = xmlDocument.CreateElement("SQL_Text"); 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;
sqltxtNode.InnerText = sqltxt;
exceptionNode.AppendChild(timeNode);
exceptionNode.AppendChild(messageNode);
exceptionNode.AppendChild(sourceNode);
exceptionNode.AppendChild(stacktraceNode);
exceptionNode.AppendChild(sqltxtNode);
xmlNode.AppendChild(exceptionNode);
}
else
{
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");
XmlElement sqltxtNode = xmlDocument.CreateElement("SQL_Text");
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;
sqltxtNode.InnerText = sqltxt;
exceptionNode.AppendChild(timeNode);
exceptionNode.AppendChild(messageNode);
exceptionNode.AppendChild(sourceNode);
exceptionNode.AppendChild(stacktraceNode);
exceptionNode.AppendChild(sqltxtNode);
xmlNode.AppendChild(exceptionNode);
} xmlDocument.Save(xmlFileName);
}
catch (Exception e)
{
throw e;
}
}
public static void AddServerInfo(String msg, String src)
{
try
{
//Get
CreateLogFile();
XmlNode xmlNode = xmlDocument.SelectSingleNode("//Server//Infomations");
if (!HasChild("Info", xmlNode))
{
XmlElement infoNode = xmlDocument.CreateElement("Info");
XmlElement timeNode = xmlDocument.CreateElement("Time");
XmlElement messageNode = xmlDocument.CreateElement("Message");
XmlElement sourceNode = xmlDocument.CreateElement("Source");
DateTime dateTime = DateTime.Now;
string messageString = msg;
string sourceString = src; timeNode.InnerText = dateTime.ToString();
messageNode.InnerText = messageString;
sourceNode.InnerText = sourceString; infoNode.AppendChild(timeNode);
infoNode.AppendChild(messageNode);
infoNode.AppendChild(sourceNode);
xmlNode.AppendChild(infoNode);
}
else
{
XmlElement infoNode = xmlDocument.CreateElement("Info");
XmlElement timeNode = xmlDocument.CreateElement("Time");
XmlElement messageNode = xmlDocument.CreateElement("Message");
XmlElement sourceNode = xmlDocument.CreateElement("Source");
DateTime dateTime = DateTime.Now;
string messageString = msg;
string sourceString = src;
timeNode.InnerText = dateTime.ToString();
messageNode.InnerText = messageString;
sourceNode.InnerText = sourceString; infoNode.AppendChild(timeNode);
infoNode.AppendChild(messageNode);
infoNode.AppendChild(sourceNode);
xmlNode.AppendChild(infoNode);
} xmlDocument.Save(xmlFileName);
}
catch (Exception e)
{
throw e;
}
}
}
}