protected override void OnStart(string[] args) { Debug.WriteLine("{0}:{1}.{2}", this.GetType().Namespace, this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name); try { var UpdateIntervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateIntervalMinutes"]); var ServerNotReachableRetries = Convert.ToInt32(ConfigurationManager.AppSettings["ServerNotReachableRetries"]); String logFile = ConfigurationManager.AppSettings["LogFile"]; if (2 <= args.Length) logFile = args[0]; Trace.Assert(!string.IsNullOrWhiteSpace(logFile), "LogFile: Parameter validation FAILED."); String scriptFile = ConfigurationManager.AppSettings["ScriptFile"]; if (2 <= args.Length) scriptFile = args[1]; Trace.Assert(!string.IsNullOrWhiteSpace(scriptFile), "ScriptFile: Parameter validation FAILED."); _worker = new LogShipperWorker(); _worker.Start(logFile, scriptFile); } catch (Exception ex) { var msg = String.Format("{0}@{1}: {2}\r\n{3}", ex.GetType().Name, ex.Source, ex.Message, ex.StackTrace); Debug.WriteLine(msg); Environment.FailFast(msg, ex); } base.OnStart(args); }
public void DoStartInvalidDirectoryThrowsDirectoryNotFoundException() { LogShipperWorker worker = new LogShipperWorker(); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; worker.Start("C:\\non-existent-directory\\non-existant-file.log", scriptFile); }
public void DoStartNullThrowsArgumentNullException() { LogShipperWorker worker = new LogShipperWorker(); worker.Start(null, null); }
public void DoStartEmptyThrowsArgumentNullException() { LogShipperWorker worker = new LogShipperWorker(); worker.Start("", ""); }
public void DoStartThrowsInvalidOperationException() { LogShipperWorker worker = new LogShipperWorker(); worker.Start(); }
public void DoAppendManyLinesThrowsTimeoutException() { LogShipperWorker worker = new LogShipperWorker(); var tempFile = Path.Combine(Directory.GetCurrentDirectory(), String.Concat(Path.GetRandomFileName(), ".log")); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(tempFile, scriptFile); System.Threading.Thread.Sleep(1000); try { using (StreamWriter streamWriter = File.AppendText(tempFile)) { streamWriter.WriteLine("{0} - This is a line of text.", -1); streamWriter.Flush(); //streamWriter.Close(); if (!File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File not found. Create operation FAILED."), tempFile); } } System.Threading.Thread.Sleep(1000); using (StreamWriter streamWriter = File.AppendText(tempFile)) { for (var c = 1000; c < 15000; c++) { streamWriter.WriteLine("{0} - This is a line of text.", c); streamWriter.Flush(); } } System.Diagnostics.Trace.WriteLine("WriteLine completed."); System.Threading.Thread.Sleep(1000); worker.Stop(); } finally { if (null != tempFile) File.Delete(tempFile); if (File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File exists. Delete operation FAILED."), tempFile); } } }
public void DoRenameFile() { LogShipperWorker worker = new LogShipperWorker(); var path = Path.Combine(Directory.GetCurrentDirectory(), "some-file.log"); var tempFile = Path.Combine(Directory.GetCurrentDirectory(), String.Concat(Path.GetRandomFileName(), ".log")); var newFile = Path.Combine(Directory.GetCurrentDirectory(), String.Concat(Path.GetRandomFileName(), ".log")); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(tempFile, scriptFile); try { using (StreamWriter streamWriter = File.AppendText(tempFile)) { streamWriter.WriteLine("This is a line of text on the original file name."); streamWriter.Flush(); if (!File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File not found. Create operation FAILED."), tempFile); } } File.Move(tempFile, newFile); if (!File.Exists(newFile)) { throw new FileNotFoundException(String.Format("newFile: File not found. Rename operation FAILED."), newFile); } using (StreamWriter streamWriter = File.AppendText(newFile)) { streamWriter.WriteLine("This is a line of text on the new file name."); streamWriter.Flush(); } } finally { worker.Stop(); if (null != tempFile) File.Delete(tempFile); if (File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File exists. Delete operation FAILED."), tempFile); } if (null != newFile) File.Delete(newFile); if (File.Exists(newFile)) { throw new FileNotFoundException(String.Format("newFile: File exists. Delete operation FAILED."), newFile); } } }
public void DoAppendText2() { LogShipperWorker worker = new LogShipperWorker(); var tempFile = Path.Combine(Directory.GetCurrentDirectory(), String.Concat(Path.GetRandomFileName(), ".log")); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(tempFile, scriptFile); try { using (StreamWriter streamWriter = File.AppendText(tempFile)) { streamWriter.WriteLine("This is the first line of text."); streamWriter.Flush(); if (!File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File not found. Create operation FAILED."), tempFile); } System.Threading.Thread.Sleep(1000); streamWriter.WriteLine("This is the second line of text."); streamWriter.WriteLine("This is the third line of text."); streamWriter.Flush(); } System.Threading.Thread.Sleep(5000); worker.Stop(); } finally { if (null != tempFile) File.Delete(tempFile); if (File.Exists(tempFile)) { throw new FileNotFoundException(String.Format("tempFile: File exists. Delete operation FAILED."), tempFile); } } }
public void DoUpdateInvalidScriptFileThrowsFileNotFoundException() { LogShipperWorker worker = new LogShipperWorker(); var logFile = Path.Combine(Directory.GetCurrentDirectory(), "some-file.log"); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(logFile, scriptFile); fReturn = worker.Update(logFile, "C:\\non-existent-directory\\non-existant-file.ps1"); }
public void DoUpdateNullThrowsArgumentNullException() { LogShipperWorker worker = new LogShipperWorker(); var logFile = Path.Combine(Directory.GetCurrentDirectory(), "some-file.log"); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(logFile, scriptFile); fReturn = worker.Update(null, null); }
public void DoUpdateWithPathReturnsTrue() { LogShipperWorker worker = new LogShipperWorker(); var logFile = Path.Combine(Directory.GetCurrentDirectory(), "some-file.log"); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; var fReturn = worker.Start(logFile, scriptFile); fReturn = worker.Update(logFile, scriptFile); Assert.AreEqual(true, fReturn); }
public void DoUpdateReturnsFalse() { LogShipperWorker worker = new LogShipperWorker(); var fReturn = worker.Update(); Assert.AreEqual(false, fReturn); }
public void DoStartInvalidDirectoryNameThrowsArgumentException() { LogShipperWorker worker = new LogShipperWorker(); var scriptFile = System.Configuration.ConfigurationManager.AppSettings["ScriptFile"]; worker.Start("C:\\invalid-directory-\t\\irrelevant-file.log", scriptFile); }
public void DoStartInvalidFileNameThrowsArgumentException() { LogShipperWorker worker = new LogShipperWorker(); worker.Start("C:\\irrelevant-directory\\irrelevant-file.log", "invalid-file-???.ps1"); }
public void DoStartInvalidScriptFileThrowsFileNotFoundException() { LogShipperWorker worker = new LogShipperWorker(); worker.Start("C:\\non-existent-directory\\non-existant-file.log", "C:\\non-existent-directory\\non-existant-file.ps1"); }