public void TestFail() { // Define variables and constants // Set up context // Execute Assure.Fail("Unit test"); // Assert outcome }
/// <summary> /// Sets the new location for the log file. If a file was already open, it should be flushed and closed before the new one is opened. /// </summary> /// <param name="filePath">The location of the new file to open.</param> /// <exception cref="ObjectDisposedException">Thrown if <see cref="ILoggingProvider.IsDisposed"/> is true.</exception> /// <exception cref="UnauthorizedAccessException">Thrown if the file could not be opened because the application does /// not have the relevant permissions.</exception> /// <exception cref="IOException">Thrown if the file could not be opened for writing.</exception> /// <exception cref="ArgumentException">Thrown if <paramref name="filePath"/> does not represent a valid file path.</exception> public virtual void SetLogFileLocation(string filePath) { lock (InstanceMutationLock) { if (IsDisposed) { throw new ObjectDisposedException("This logging provider has been disposed: Can not set new log file location!"); } if (!IOUtils.IsValidFilePath(filePath)) { throw new ArgumentException("'" + filePath + "' is not a valid file path!", "filePath"); } DisposeCurrentStream(); #if !RELEASE && !DEVELOPMENT try { currentFileStreamWriter = File.CreateText(filePath); } catch (Exception e) { Assure.Fail("Could not create log file at '" + filePath + "'. Exception: " + e.GetAllMessages()); throw; } #else currentFileStreamWriter = File.CreateText(filePath); #endif #if !RELEASE && !DEVELOPMENT string openingMessage = "Platform version " + LosgapSystem.Version + ", debug build"; #elif DEVELOPMENT string openingMessage = "Platform version " + LosgapSystem.Version + ", development build"; #else var version = Assembly.GetExecutingAssembly().GetName().Version; string openingMessage = "Build " + version.Build + "." + version.Revision; #endif currentFileStreamWriter.Write(LosgapSystem.ApplicationName + ", made by Egodystonic Studios " + "( http://www.escapelizards.com | http://www.egodystonic.com ) :: "); currentFileStreamWriter.WriteLine(openingMessage); currentFileStreamWriter.WriteLine("Installation directory set to " + LosgapSystem.InstallationDirectory.FullName + "."); currentFileStreamWriter.WriteLine("Mutable data directory set to " + LosgapSystem.MutableDataDirectory.FullName + "."); currentFileStreamWriter.WriteLine("Working directory is " + Environment.CurrentDirectory + "."); currentFileStreamWriter.WriteLine("Logical core count is " + Environment.ProcessorCount + "."); currentFileStreamWriter.WriteLine(); currentFileStreamWriter.Flush(); } }
public virtual void WriteMessage(LogMessageSeverity messageSeverity, string message, Exception associatedException, string callerMemberName, string callerFilePath, int callerLineNumber) { Assure.NotNull(message); Assure.NotNull(callerMemberName); Assure.NotNull(callerFilePath); message = message.Replace(Environment.NewLine, Environment.NewLine + STACK_TRACE_LINE_PREFIX); lock (InstanceMutationLock) { if (IsDisposed) { return; // Safest option: No point throwing exceptions if some random component elicits a warning on shutdown } if (currentFileStreamWriter == null) { throw new InvalidOperationException("Can not write log line: No log file location has successfully been set on this logging provider!"); } logLineBuilder.Clear(); if (AddTimestamps) { logLineBuilder.Append(DateTime.Now.ToString(TIMESTAMP_FORMAT)); logLineBuilder.Append("\t"); } switch (messageSeverity) { case LogMessageSeverity.Standard: logLineBuilder.Append(STANDARD_LOG_PREFIX); break; case LogMessageSeverity.Debug: logLineBuilder.Append(DEBUG_LOG_PREFIX); break; case LogMessageSeverity.Warning: logLineBuilder.Append(WARNING_LOG_PREFIX); break; case LogMessageSeverity.Fatal: logLineBuilder.Append(FATAL_LOG_PREFIX); break; } logLineBuilder.Append(message); if (AppendCaller) { logLineBuilder.Append(" <= "); logLineBuilder.Append(callerMemberName); if (IncludeDetailedCallerInfo) { logLineBuilder.Append("() in "); logLineBuilder.Append(Path.GetFileName(callerFilePath)); logLineBuilder.Append(":"); logLineBuilder.Append(callerLineNumber); } else { logLineBuilder.Append("()"); } } if (associatedException != null) { logLineBuilder.AppendLine(); associatedException.ForEachException(e => { logLineBuilder.Append(ASSOCIATED_EXCEPTION_PREFIX); logLineBuilder.AppendLine(associatedException.GetAllMessages(true).Replace(Environment.NewLine, " ")); if (e is ExternalException) { logLineBuilder.Append(STACK_TRACE_LINE_PREFIX); logLineBuilder.AppendLine("** External exception code: 0x" + (e as ExternalException).ErrorCode.ToString("x").ToUpper()); } if (e.StackTrace != null) { foreach (string line in e.StackTrace.Split(Environment.NewLine) .Select(line => line.Trim()) .Where(line => !line.IsNullOrWhiteSpace())) { logLineBuilder.Append(STACK_TRACE_LINE_PREFIX); logLineBuilder.AppendLine(line); } } else { logLineBuilder.Append(STACK_TRACE_LINE_PREFIX); logLineBuilder.AppendLine("<No stack trace>"); } }); } logLineBuilder.AppendLine(); #if !RELEASE && !DEVELOPMENT try { currentFileStreamWriter.Write(logLineBuilder); currentFileStreamWriter.Flush(); } catch (Exception e) { Assure.Fail("Could not write message to log file. Exception: " + e.GetAllMessages()); throw; } #else currentFileStreamWriter.Write(logLineBuilder); currentFileStreamWriter.Flush(); #endif BroadcastMessage(messageSeverity, message, associatedException); } }