public void CloseFileFromWriting(TestExecution testExecution) { // WARNING: testExecution may be null if (writer != null) { try { writer.Close(); } catch (Exception e) { string msg = "Unable to close data log file " + mFile + ". Error='" + e.Message + "'"; if (testExecution != null) { testExecution.LogErrorWithTimeFromTrigger(msg); } TestSequence().LogError(msg); return; } try { writer.Dispose(); // can this throw exceptions? if so, it isn't fatal to the test execution, so we catch it here } catch (Exception e) { string msg = "Unable to dispose data log file " + mFile + ". Error='" + e.Message + "'"; if (testExecution != null) { testExecution.LogErrorWithTimeFromTrigger(msg); } TestSequence().LogError(msg); } writer = null; } }
public void AddLine(TestExecution testExecution) { DateTime now = DateTime.Now; String seperator = ", "; String theEntry = now.ToShortDateString() + seperator + now.ToLongTimeString(); try { foreach (DataValueDefinition value in mValuesToLog) { theEntry += seperator + testExecution.DataValueRegistry.GetObject(value.Name).Value; } } catch (Exception e) { string msg = "Unable to build data log entry for " + mFile + ". Error='" + e.Message + "'"; testExecution.LogErrorWithTimeFromTrigger(msg); TestSequence().LogError(msg); } testExecution.LogMessageWithTimeFromTrigger(Name + " is logging '" + theEntry + "'"); if (writer == null) { OpenFileForWriting(testExecution); } if (writer != null) { try { writer.WriteLine(theEntry); } catch (Exception e) { string msg = "Unable to write entry to data log file " + mFile + ". Error='" + e.Message + "'"; testExecution.LogErrorWithTimeFromTrigger(msg); TestSequence().LogError(msg); } if (mFlushAfterEachLine) { writer.Flush(); } if (mCloseAfterEachLine) { CloseFileFromWriting(testExecution); } } }
public void OpenFileForWriting(TestExecution testExecution) { // WARNING: testExecution may be null try { if (writer != null) { CloseFileFromWriting(testExecution); } } catch (Exception e) { string msg = "Unable to successfully close the data log file before re-opening. Error='" + e.Message + "'"; if (testExecution != null) { testExecution.LogErrorWithTimeFromTrigger(msg); } TestSequence().LogError(msg); } string filePath = FileHelper.ExpandPath(this, mFile); string fileName = Path.GetFileName(filePath); filePath = Path.GetDirectoryName(filePath); if (!Directory.Exists(filePath)) { try { Directory.CreateDirectory(filePath); } catch (Exception e) { throw new ArgumentException("Unable to open data log file '" + filePath + "'. Reason=Unable to create path. Low-level message=" + e.Message); } } try { writer = new StreamWriter(filePath + "\\" + fileName, true); // true=append } catch (Exception e) { string msg = "Unable to open the data log file " + mFile + ". Error='" + e.Message + "'"; if (testExecution != null) { testExecution.LogErrorWithTimeFromTrigger(msg); } TestSequence().LogError(msg); } }