class Logger
{
public void WriteEventLog(string errorMessage)
{
StringBuilder errorBuilder = new StringBuilder();
try
{
if (!string.IsNullOrEmpty(errorMessage))
{
string fileDateFormat = "yyyyMMdd";
string fileDirectory = ConfigurationManager.AppSettings["FileLocation"];
string errorLogFilePath = fileDirectory + "ErrorLog";
if (!Directory.Exists(errorLogFilePath))
{
Directory.CreateDirectory(errorLogFilePath);
}
errorLogFilePath = errorLogFilePath + @"\ErrorLogFile" + DateTime.Now.ToString(fileDateFormat) + ".log";
errorBuilder.Append("-------------------------------------------------------------------------------------------------\n");
errorBuilder.AppendLine(System.DateTime.Now.ToString());
errorBuilder.AppendLine("Error Message :" + errorMessage);
errorBuilder.AppendLine("-------------------------------------------------------------------------------------------------");
//Writing to Text File.
TextWriter oTW = System.IO.File.AppendText(errorLogFilePath);
oTW.WriteLine(errorBuilder.ToString());
oTW.Close();
}
}
catch (Exception exec)
{
throw exec;
}
}
}
{
public void WriteEventLog(string errorMessage)
{
StringBuilder errorBuilder = new StringBuilder();
try
{
if (!string.IsNullOrEmpty(errorMessage))
{
string fileDateFormat = "yyyyMMdd";
string fileDirectory = ConfigurationManager.AppSettings["FileLocation"];
string errorLogFilePath = fileDirectory + "ErrorLog";
if (!Directory.Exists(errorLogFilePath))
{
Directory.CreateDirectory(errorLogFilePath);
}
errorLogFilePath = errorLogFilePath + @"\ErrorLogFile" + DateTime.Now.ToString(fileDateFormat) + ".log";
errorBuilder.Append("-------------------------------------------------------------------------------------------------\n");
errorBuilder.AppendLine(System.DateTime.Now.ToString());
errorBuilder.AppendLine("Error Message :" + errorMessage);
errorBuilder.AppendLine("-------------------------------------------------------------------------------------------------");
//Writing to Text File.
TextWriter oTW = System.IO.File.AppendText(errorLogFilePath);
oTW.WriteLine(errorBuilder.ToString());
oTW.Close();
}
}
catch (Exception exec)
{
throw exec;
}
}
}
No comments:
Post a Comment