/// <summary>Common implementation method for Noting that Fault has occurred, with or without a related System.Exception ex.</summary> public static void NoteFaultOccurance(string faultDesc, System.Exception ex, AssertType assertType, System.Diagnostics.StackFrame sourceFrame) { if (ex == null) AssertCommon(Fcns.CheckedFormat("AssertFault:{0}", faultDesc), assertType, sourceFrame); else AssertCommon(Fcns.CheckedFormat("AssertFault:{0} with exception:{1}", faultDesc, ex.Message), assertType, sourceFrame); }
/// <summary> This is the inner-most implementation method for the Assert helper class. It implements all of the assertType specific behavior for all assertions that get triggered.</summary> private static void AssertCommon(string mesg, AssertType assertType, System.Diagnostics.StackFrame sourceFrame) { // always log all triggered asserts to the BasicFallbackLog string logStr = Fcns.CheckedFormat("{0} at file:'{1}', line:{2}", mesg, sourceFrame.GetFileName(), sourceFrame.GetFileLineNumber()); if (assertType != AssertType.Log) { if (EnableBasicFallbackLogging) Logging.BasicFallbackLogging.LogError(logStr); if (queuedAssertLogger == null) // in an MT world we might create a few of these simultaneously. This is not a problem as the distribution engine supports such a construct so locking is not required here in order to get valid behavior. queuedAssertLogger = new Logging.QueuedLogger("MosaicLib.Utils.Assert"); queuedAssertLogger.Emitter(DefaultAssertLoggingMesgType).Emit(logStr); } bool ignoreFault = false; // intended to be used by debug user to ignore such asserts on a case by case basis if (assertType == AssertType.Log) { if (assertLogger == null) // in an MT world we might create a few of these simultaneously. This is not a problem as the distribution engine supports such a construct so locking is not required here in order to get valid behavior. assertLogger = new Logging.Logger("MosaicLib.Utils.Assert"); assertLogger.Emitter(DefaultAssertLoggingMesgType).Emit(logStr); return; } else if (assertType == AssertType.LogFallback) { return; // already done } else if (assertType == AssertType.ThrowException) { if (!ignoreFault) throw new AssertException(mesg, sourceFrame); return; } if (!ignoreFault) { // the remaining types always trigger a breakpoint if a debugger is attached and the hosting environment has set the EnabledAssertDebugBreakpoints flag if (System.Diagnostics.Debugger.IsAttached && EnableAssertDebugBreakpoints) System.Diagnostics.Debugger.Break(); } }
/// <summary>Converts the given DateTime value to a string using the given summary desired format</summary> /// <param name="dt">Specifies the DateTime value to convert</param> /// <param name="dtFormat">Specifies the desired format from the set of supported enum values.</param> /// <returns>The DateTime converted to a string based on the desired format.</returns> public static string CvtToString(ref DateTime dt, DateTimeFormat dtFormat) { string result = string.Empty; switch (dtFormat) { default: case DateTimeFormat.LogDefault: result = Fcns.CheckedFormat("{0}-{1}-{2} {3}:{4}:{5}.{6}", dt.Year.ToString("D4"), dt.Month.ToString("D2"), dt.Day.ToString("D2"), dt.Hour.ToString("D2"), dt.Minute.ToString("D2"), dt.Second.ToString("D2"), dt.Millisecond.ToString("D3")); break; case DateTimeFormat.ShortWithMSec: result = Fcns.CheckedFormat("{0}{1}{2}_{3}{4}{5}.{6}", dt.Year.ToString("D4"), dt.Month.ToString("D2"), dt.Day.ToString("D2"), dt.Hour.ToString("D2"), dt.Minute.ToString("D2"), dt.Second.ToString("D2"), dt.Millisecond.ToString("D3")); break; } return(result); }
/// <summary>Common implementation method for Noting that a condition test failed.</summary> public static void NoteConditionCheckFailed(string condDesc, AssertType assertType, System.Diagnostics.StackFrame sourceFrame) { AssertCommon(Fcns.CheckedFormat("AssertCondition:[{0}] Failed", condDesc), assertType, sourceFrame); }