none
C# Console App's Log File Read not working with task scheduler RRS feed

  • Question

  • 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


    Saturday, October 6, 2018 3:01 AM

Answers

All replies

  • Hello,

    If it work from Command Prompt - program is OK.

    Problem - require permission for account on which it should run in Scheduler to use a share.


    Sincerely, Highly skilled coding monkey.

    Saturday, October 6, 2018 10:01 AM
  • I tried with Filestream (commented out) code with FileAccess.ReadWrite. .it did not help. I chose an option for StreamReader and StreamWriter. How to find the account of the task scheduler ? When i setup the task schedular, i chose my account with my alias, and chosen Run Whether the user logged in or not, and opted Do not store password.

    Let me know if you need any information.

    Regards

    Madhavan.TR


    Madhavan Thasu Ramachandran

    Saturday, October 6, 2018 6:42 PM
  • "Do not store password. The task will only have access to local computer resources."
    So an access denied when accessing a network share is not surprising.
    And please do never ever code like this:
                catch (Exception ex)
                {

                }


    Saturday, October 6, 2018 7:48 PM
  • Never mind.. fix the issue in the code.

    Thanks all.

    Regards


    Madhavan Thasu Ramachandran

    Sunday, October 7, 2018 2:28 AM