Thursday, May 21, 2009

Error Log

XML Format.

1)Create a .cs file.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Text;


///


/// Summary description for AppLogger
///

public static class AppLogger
{
// Fields
private static XmlDocument LogDoc;
private static XmlElement LogElement;
private static XmlNode LogNode;

// Methods
static AppLogger()
{
try
{
LogDoc = new XmlDocument();
string path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ErrorFolder"] +"\\"+ DateTime.Now.ToString("dd-MM-yyyy") + ".xml");

if (!File.Exists(path))
{
XmlTextWriter writer = new XmlTextWriter(path, Encoding.Unicode);
writer.WriteStartDocument(true);
writer.WriteStartElement("LOGGER");
writer.WriteEndElement();
writer.Close();
}
try
{
LogDoc.Load(path);
}
catch
{

}
}
catch
{
}
}

public static void LogError(string Message, Severity severity)
{
try
{
if(Message!="Thread was being aborted.")
{
//in the web config specify the path(where to store log file like ()).
string path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ErrorFolder"] + "\\" + DateTime.Now.ToString("dd-MM-yyyy") + ".xml");


if (!File.Exists(path))
{
XmlTextWriter writer = new XmlTextWriter(path, Encoding.Unicode);
writer.WriteStartDocument(true);
writer.WriteStartElement("LOGGER");
writer.WriteEndElement();
writer.Close();
LogDoc = new XmlDocument();
LogDoc.Load(path);
}
LogNode = LogDoc.DocumentElement;
LogElement = LogDoc.CreateElement("ELOG");
Message = Message.Replace("&", "&");
Message = Message.Replace("<", "<");
Message = Message.Replace(">", ">");
LogElement.InnerXml = "" + severity.ToString() + "" + Message + "";
LogNode.AppendChild(LogElement);

LogDoc.Save(path);
}
}
catch
{
}

}

// Nested Types
public enum Severity
{
Error = 2,
Failure = 3,
Informational = 1
}
}

on the aspx/ascx in a catch block write below lines

2) AppLogger.LogError("User Edit Profile, Sending Mail() " + ex.Message, AppLogger.Severity.Error);

TXT format
1)Create .cs file

public static void ErrorLogFile(string strErrorMessage)
{
try
{
string strPath = "~/LogFile/ErrorDetails/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(strPath)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(strPath)).Close();
}
using (StreamWriter sWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(strPath)))
{
sWriter.WriteLine("\r\nLog Entry : ");
sWriter.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
string strError = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
". Error Message:" + strErrorMessage;
sWriter.WriteLine(strError);
sWriter.WriteLine("__________________________");
sWriter.Flush();
sWriter.Close();
}
}
catch (Exception ex)
{
ErrorLogFile(ex.Message);
}

}
2)call this file catch block
clsLogFile.ErrorLogFile(ex.Message);

The same way we can track the mail sent details from the server.

Copyright © 2009 Angel