Thursday, February 4, 2010

HOW TO CREATE ERRORLOG FILE

catch(Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
LCE.Configuration.ErrorLog("Payment/PaymentList.cs", errorMessage);
}

CONFIGURATION.CS IN APP_Code folder FOR Database connection and ERROR LOGS and GET ROOT PATH

CREAe ERROR LOGS FOLDER IN ROOT PATH

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//
using System.IO;
using System.Collections;



namespace LCE
{
using System.Web.UI;
using System.IO;
using System.Collections;
using System.Web.Security;
using System.Web;
using System.Data;
using System.Configuration;
using System;


public class Configuration
{
public static string GetConnnectionString()
{
return ConfigurationSettings.AppSettings["ConnectionString"];
}

public static string GetCurrentUSerID()
{
return HttpContext.Current.User.Identity.Name;
}
public static string GetRootPath()
{
return ConfigurationSettings.AppSettings["rootPath"];
}
public static void ErrorLog(string sFilename, string sErrMsg)
{
string sPathName="";
sPathName = System.AppDomain.CurrentDomain.BaseDirectory;
sPathName = sPathName + "ErrorLogs" + "\\ErrorLog_";
string sErrorTime;
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear +"_"+ sMonth +"_"+ sDay+".txt";
if (File.Exists(sPathName + sErrorTime))
{
File.SetAttributes(sPathName + sErrorTime, FileAttributes.Normal);//set file attributes readonly to normal.
}
using (StreamWriter objsw = new StreamWriter(sPathName + sErrorTime, true))
{
objsw.WriteLine("*****************Start*****************");
objsw.WriteLine();
objsw.WriteLine("Date/Time: " + System.DateTime.Now.ToString());
objsw.WriteLine("File Name :" + sFilename);
objsw.WriteLine("Error Msg :" + sErrMsg);
objsw.WriteLine();
objsw.WriteLine("******************End********************");
objsw.Flush();
objsw.Close();
}
}
}
}

No comments:

Post a Comment