public static void Logs(string Logtext, string Status)
{
ISelenium Selenium = GetSeleniumInstance();
string LogMessage;
string StatusTest = Status.ToUpper();
string time = Time_Stamp();
String DriveName = ConfigurationManager.AppSettings["LogDriveName"]; //
String ErrorFileName = String.Concat(DriveName, ":\\Error_History.html");
try
{
String logsFileName = string.Empty;
switch (StatusTest)
{
case "PASSED":
logsFileName = String.Concat(DriveName, ":\\Passed_Results.html");
LogMessage = string.Format("{0},\" \"{1},\" \"{2}", Logtext, time, StatusTest);
break;
case "FAILED":
logsFileName = String.Concat(DriveName, ":\\Failed_Results.html");
string error = Generate_Unique_variable("Error") + ".png";
Selenium.CaptureScreenshot(DriveName + ":\\Selenium_Screenshots\\" + error);
LogMessage = string.Format("{0},\" \"{1},\" \"{2}", Logtext, time, StatusTest);
break;
default:
logsFileName = String.Concat(DriveName, ":\\Failed_Results.html");
LogMessage = string.Format("{0},\" \"{1},\" \"{2}", Logtext, time, StatusTest);
break;
}
WriteLogs(logsFileName, LogMessage);//write logs to appropriate log file.
WriteLogs(ErrorFileName, LogMessage);// write all the logs to error history
}
catch (Exception ex)
{
WriteLogs(string.Concat(DriveName, ":\\Failed_Results.html"),String.Concat(ex.Message,"FAILED"));
}
}
// WriteLogs Method
private static void WriteLogs(string FileName, String LogMessage)
{
//Delete if the file is old
if (File.GetLastAccessTime(FileName) < DateTime.Now.Subtract(new TimeSpan(24, 0, 0)))
//if the file does not exist, GetLastAccessTime returns minimum datetime value
{
File.Delete(FileName);
};
LogMessage = String.Concat(LogMessage, "\r\n");
//Append The Message To The File
using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileName, true))
{
file.WriteLine(LogMessage);
file.Close();
}
}
You can call this generic method by using:
Logs("The Log generated.", "PASSED"); or
Logs("The Log not generated.", "FAILED");
No comments:
Post a Comment