Logging class

19/12/2011 17:54

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Xml.Linq;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace CASA.Utils
{
public class Logging
{
//Write Logs in XML File
public static void WriteErrorLog(string errorMsg, string source, string stacktrace)
{
try
{

string appDir = Application.StartupPath.ToLower().Replace("\\bin\\debug", "") + "\\Log\\watchdawg.xml";

string _FilePath = Application.StartupPath.ToLower().Replace("\\bin\\debug", "") + "\\Log\\" + "Error_"+ DateTime.Now.ToString("MMMM-dd-yyyy") + ".txt";

if (!File.Exists(_FilePath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(_FilePath))
{
sw.WriteLine("------------------------------------------------------------------------------------------");
sw.WriteLine("Spermsoft v3.0: New Log Created");
sw.WriteLine("------------------------------------------------------------------------------------------");
sw.Close();
}
}

using (System.IO.StreamWriter Writer = new System.IO.StreamWriter(_FilePath, true))
{
//Writer.WriteLine("------------------------------------------------------------------------------------------");
string lNewText = DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss") + " : ";
Writer.WriteLine(lNewText);
Writer.WriteLine("Error Message: " + errorMsg);
Writer.WriteLine("Source: " + source);
Writer.WriteLine("ST: " + stacktrace);
Writer.WriteLine("******************************************************************************************");
Writer.Close();
}

//XDocument xmlDoc = XDocument.Load(appDir);

//xmlDoc.Element("ErrorFile").Add
// (new XElement("Error",
// new XElement("Date", DateTime.Now.ToString("MMM-dd-yyyy")),
// new XElement("Time", DateTime.Now.ToString("hh:mm:ss tt")),
// new XElement("ErrorMessage", errorMsg),
// new XElement("Source", source),
// new XElement("StackTrace", stacktrace)));

//xmlDoc.Save(appDir);
}

catch (Exception ex)
{
throw new Exception(ex.Message);
}

}

//Write Logs in XML File
public static void WriteLog(string message, string user)
{
try
{


string appDir = Application.StartupPath.ToLower().Replace("\\bin\\debug", "") + "\\Log\\Log.xml";
string _FilePath = Application.StartupPath.ToLower().Replace("\\bin\\debug", "") + "\\Log\\"+DateTime.Now.ToString("MMMM-dd-yyyy")+".txt";

if (!File.Exists(_FilePath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(_FilePath))
{
sw.WriteLine("New Log Created");
sw.Close();
}
}

using (System.IO.StreamWriter Writer = new System.IO.StreamWriter(_FilePath, true))
{
string lNewText = DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss")+" "+user+ " : "+ message;
Debug.WriteLine(lNewText);
Writer.WriteLine(lNewText);
Writer.Close();
}

//XDocument xmlDoc = XDocument.Load(appDir);

//xmlDoc.Element("LogFile").Add
// (new XElement("Log",
// new XElement("Date", DateTime.Now.ToString("MMMM-dd-yyyy hh:mm:ss tt")),
// new XElement("User", user),
// new XElement("Message", message)));

//xmlDoc.Save(appDir);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}


}
}