using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Diagnostics;
using System.Text;
/// <summary>
/// Summary description for ErrorLog
/// </summary>
public class ErrorLog
{
public ErrorLog()
{
//
// TODO: Add constructor logic here
//
}
public static bool WriteErrorLog(string strErrorFileName, string strErrorString)
{
/*---------------------------------------------------------------------------
Function Name : WriteErrorLog
Arguments : strErrorFile, strError
Return Type : bool
Description : Write Error Log in Text File
---------------------------------------------------------------------------*/
bool boolReturn = false;
strErrorString = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + ": " + strErrorString.ToString();
try
{
if (strErrorString.ToUpper().Substring(0, strErrorString.Length - 1) != "THREAD WAS BEING ABORTED")
boolReturn = IsWriteLog(strErrorFileName, strErrorString); //Write Error Log in Text File
}
catch (Exception)
{
}
return boolReturn;
}
public static bool WriteErrorLog(string strErrorString)
{
/*---------------------------------------------------------------------------
Function Name : WriteErrorLog
Arguments : strErrorFile, strError
Return Type : bool
Description : Write Error Log in Text File
---------------------------------------------------------------------------*/
string strErrorFileName = @"~\ErrorLogs\" + (DateTime.Today.ToLongDateString());
bool boolReturn = false;
strErrorString = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + ": " + strErrorString.ToString();
try
{
if (strErrorString.ToUpper().Substring(0, strErrorString.Length - 1) != "THREAD WAS BEING ABORTED")
boolReturn = IsWriteLog(strErrorFileName, strErrorString); //Write Error Log in Text File
}
catch (Exception)
{
}
return boolReturn;
}
private static bool IsWriteLog(string strErrorFile, string strError)
{
/*---------------------------------------------------------------------------
Function Name : IsWriteLog
Arguments : strErrorFile, strError
Return Type : bool
Description : Create Text file and write Errors.
---------------------------------------------------------------------------*/
StreamWriter swErrorFile;
FileInfo fiFileInfo;
int intMaxLogSize;
bool boolReturn;
// Get maximum error log size from web.config, use default of 100K if not found.
intMaxLogSize = 1000000;
try
{
if (Directory.Exists(HttpContext.Current.Server.MapPath(@"~\ErrorLogs\")) == false)
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(@"~\ErrorLogs\"));
}
// Set reference to passed error file.
fiFileInfo = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(strErrorFile + ".log"));
if (fiFileInfo.Exists)
{
// If current error file larger than max size.
if (fiFileInfo.Length > intMaxLogSize)
{
// Copy current error log to new file appending '_old' to current name,
// overwrite file if already exist.
swErrorFile = fiFileInfo.CreateText();
fiFileInfo.CopyTo(swErrorFile + "_" + DateTime.Now.Date.ToShortDateString() + ".log", true);
// Open stream to overwrite current error log file.
}
else
{
// Open stream to append to current error log file.
swErrorFile = fiFileInfo.AppendText();
}
}
else
{
// Create and open stream to error log file.
swErrorFile = fiFileInfo.AppendText();
}
//Write to error log and close stream
swErrorFile.WriteLine(strError);
swErrorFile.WriteLine();
swErrorFile.Close();
return boolReturn = true;
}
catch (Exception)
{
boolReturn = false;
}
return boolReturn;
}
#region Log Exception in Error Log
public static void Log(Exception exception)
{
/*-----------------------------------------------------------------------------------------------
Function Name : Log
Arguments : exception
Return Type : void
Description : Logs the Exception in a log file which is generated in the name of the aspx page
-----------------------------------------------------------------------------------------------*/
string strFileName = DateTime.Today.ToLongDateString();
string strErrorLogPath = (HttpContext.Current.Server.MapPath(@"~\ErrorLogs\") + (strFileName + ".log"));
string strErrorSrcFileName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.CurrentExecutionFilePath);
string strMethodName = new StackTrace().GetFrame(1).GetMethod().Name;
string strUserName = HttpContext.Current.Session["LoginName"] == null ? " " : HttpContext.Current.Session["LoginName"] as string;
string strErrorMessage;
TextWriter twErrorFile;
strErrorMessage = (DateTime.Now.ToString() + "\r\n"); ;
strErrorMessage += "File Name :" + strErrorSrcFileName + "\r\n";
strErrorMessage += "Method Name :" + strMethodName + "\r\n";
strErrorMessage += "Error Text :" + exception.Message + "\r\n";
strErrorMessage += "Error Desc :" + exception.StackTrace + "\r\n";
strErrorMessage += "User Name :" + strUserName + "\r\n";
strErrorMessage += "-----------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n";
twErrorFile = File.AppendText(strErrorLogPath);
twErrorFile.WriteLine(strErrorMessage);
twErrorFile.Close();
}
#endregion
}
No comments:
Post a Comment