M
Madhavan thasuramachandran
Guest
Hi All,
I am facing a weird issue with my console app. Here is the scenario. My console app, copies a sql backup file to a network share. I do write a simple log file to write the information where the application is, like, "Copy Started", "Copy in Progress", "Copy Completed". Writing to the log file working fine. I have to check if the file is already copied to the network share using the log information., for example "Log Completed".. then do not run the application. Everything works good if run the application from the command prompt or by running the app directly. But when i configure exe in a Task scheduler, it is not checking the status.
Here is the code..Let me know where i am making mistake..LogWriter have two set of read/write codes
static void Main(string[] args)
{
LogWriter logWriter = new LogWriter();
logWriter.LogWrite("Copy Started");
try
{
if (logWriter.CheckUploadStatus() == false)
{
logWriter.LogWrite("Copy Completed");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
--
public class LogWriter
{
private string m_exePath = string.Empty;
private static string LogFileName
{ get; set; }
bool IsUploaded = false;
public LogWriter()
{
LogFileName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name + "-" + System.DateTime.Today.ToString("MM-dd-yyyy") + ".log";
}
public void LogWrite(string logMessage)
{
try
{
String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + LogFileName;
using (StreamWriter sr = File.AppendText(path))
{
sr.WriteLine("{0} : {1}", System.DateTime.Now.ToString(), logMessage);
sr.Close();
}
//FileStream objFilestream = new FileStream(string.Format("{0}\\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), LogFileName), FileMode.Append, FileAccess.Write);
//StreamWriter objStreamWriter = new StreamWriter((Stream)objFilestream);
//objStreamWriter.WriteLine();
//objStreamWriter.Close();
//objFilestream.Close();
}
catch (Exception ex)
{
}
}
public bool CheckUploadStatus()
{
LogWriter logwriter = new LogWriter();
try
{
string logFileName = LogWriter.LogFileName;
FileInfo file = new FileInfo(logFileName);
Console.WriteLine(logFileName);
if (!file.Exists)
IsUploaded = false;
else
{
//string[] lines = File.ReadAllLines(logFileName);
//foreach (string line in lines)
//{
// if (line.Contains("Completed"))
// {
// Console.WriteLine(line);
// IsUploaded = true;
// break;
// }
//}
using (StreamReader sr = File.OpenText(logFileName))
{
String s = "";
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("Completed"))
{
IsUploaded = true;
break;
}
}
}
}
}
catch (Exception ex)
{
logwriter.LogWrite(ex.Message);
}
return IsUploaded;
}
}
Madhavan Thasu Ramachandran
Continue reading...
I am facing a weird issue with my console app. Here is the scenario. My console app, copies a sql backup file to a network share. I do write a simple log file to write the information where the application is, like, "Copy Started", "Copy in Progress", "Copy Completed". Writing to the log file working fine. I have to check if the file is already copied to the network share using the log information., for example "Log Completed".. then do not run the application. Everything works good if run the application from the command prompt or by running the app directly. But when i configure exe in a Task scheduler, it is not checking the status.
Here is the code..Let me know where i am making mistake..LogWriter have two set of read/write codes
static void Main(string[] args)
{
LogWriter logWriter = new LogWriter();
logWriter.LogWrite("Copy Started");
try
{
if (logWriter.CheckUploadStatus() == false)
{
logWriter.LogWrite("Copy Completed");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
--
public class LogWriter
{
private string m_exePath = string.Empty;
private static string LogFileName
{ get; set; }
bool IsUploaded = false;
public LogWriter()
{
LogFileName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name + "-" + System.DateTime.Today.ToString("MM-dd-yyyy") + ".log";
}
public void LogWrite(string logMessage)
{
try
{
String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + LogFileName;
using (StreamWriter sr = File.AppendText(path))
{
sr.WriteLine("{0} : {1}", System.DateTime.Now.ToString(), logMessage);
sr.Close();
}
//FileStream objFilestream = new FileStream(string.Format("{0}\\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), LogFileName), FileMode.Append, FileAccess.Write);
//StreamWriter objStreamWriter = new StreamWriter((Stream)objFilestream);
//objStreamWriter.WriteLine();
//objStreamWriter.Close();
//objFilestream.Close();
}
catch (Exception ex)
{
}
}
public bool CheckUploadStatus()
{
LogWriter logwriter = new LogWriter();
try
{
string logFileName = LogWriter.LogFileName;
FileInfo file = new FileInfo(logFileName);
Console.WriteLine(logFileName);
if (!file.Exists)
IsUploaded = false;
else
{
//string[] lines = File.ReadAllLines(logFileName);
//foreach (string line in lines)
//{
// if (line.Contains("Completed"))
// {
// Console.WriteLine(line);
// IsUploaded = true;
// break;
// }
//}
using (StreamReader sr = File.OpenText(logFileName))
{
String s = "";
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("Completed"))
{
IsUploaded = true;
break;
}
}
}
}
}
catch (Exception ex)
{
logwriter.LogWrite(ex.Message);
}
return IsUploaded;
}
}
Madhavan Thasu Ramachandran
Continue reading...