Last active
November 29, 2018 12:59
-
-
Save chrisfcarroll/4c2266ee11fb90637c18fe13dc5be896 to your computer and use it in GitHub Desktop.
Runtime Assertion, Log.Assert, TryLog, TryElse methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class LogAssert | |
{ | |
public static void Assert(this ILogger logger, Func<bool> assertion, string failureMessage = "Try failed", LogEventLevel level = LogEventLevel.Debug, string successMessage=null, IEnumerable args = null) | |
{ | |
try | |
{ | |
var ok = assertion(); | |
if (ok && successMessage!=null) { logger.Write(level, successMessage, args ?? new object[0]); } | |
if (!ok) { logger.Write(level, failureMessage, args ?? new object[0]); } | |
} | |
catch (Exception e) { logger.Write(level,e,failureMessage, args??new object[0]);} | |
} | |
public static void TryLog(this ILogger logger, Action<ILogger> action, string failureMessage = "Error trying to log", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
try { action(logger); } | |
catch (Exception e) { logger.Write(level, e, failureMessage, args ?? new object[0]); } | |
} | |
public static void LogSessionVariables(this ILogger logger, string calledFrom, HttpSessionStateBase session) | |
{ | |
logger.TryLog(l => l.Debug(calledFrom + " : Session[{@SessionID}] variables : {@Session}", session.SessionID, session)); | |
string lastKey = "notstarted"; | |
try | |
{ | |
foreach (var key_ in session.Keys) | |
{ | |
var key= lastKey = key_.ToString(); | |
logger.TryLog(l => l.Debug(calledFrom + " : Session[{@Key}]= {@Value}", key, session[key])); | |
} | |
} | |
catch (Exception e) { logger.Error(e, calledFrom + " : Last Session key enumerated was {@Key}", lastKey); } | |
} | |
} | |
public static class Assert | |
{ | |
public static bool That(bool assertion, string failureMessage = "Assert Failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
if (!assertion) logger.Write(level, failureMessage, args ?? new object[0]); | |
return assertion; | |
} | |
public static T That<T>(T actual, Predicate<T> assertion, string failureMessage = "Assert Failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args=null) | |
{ | |
if (!assertion(actual)) logger.Write(level, failureMessage, args??new object[0]); | |
return actual; | |
} | |
public static T That<T>(T actual, Predicate<T> assertion, string failureMessage, string successMessage, LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
var ok = false; | |
try { ok = assertion(actual); }catch (Exception e) { logger.Write(level, e, failureMessage, args ?? new object[0]); } | |
logger.Write(level, ok ? successMessage : failureMessage, args ?? new object[0]); | |
return actual; | |
} | |
internal static ILogger logger = LoggingConfig.Logger; | |
} | |
public static class Try | |
{ | |
public static void ElseLog(Action action, string failureMessage = "Try failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
try { action(); } | |
catch (Exception e) { Logger.Write(level, e, failureMessage, args ?? new object[0]); } | |
} | |
public static T ElseLog<T>(Func<T> action, string failureMessage = "Try failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
try{ return action(); } | |
catch (Exception e) | |
{ | |
Logger.Write(level, e, failureMessage, args ?? new object[0]); | |
return default(T); | |
} | |
} | |
public static T Else<T>(Func<T> action, T defaultValue) | |
{ | |
try { return action(); }catch{return defaultValue;} | |
} | |
public static T ElseThrow<T>(Func<T> action, string failureMessage = "Try failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
try { return action(); } | |
catch (Exception e) { Logger.Write(level, e, failureMessage, args ?? new object[0]); throw; } | |
} | |
public static void ElseThrow(Action action, string failureMessage = "Try failed", LogEventLevel level = LogEventLevel.Debug, IEnumerable args = null) | |
{ | |
try { action(); } | |
catch (Exception e){ Logger.Write(level, e, failureMessage, args ?? new object[0]); throw; } | |
} | |
internal static ILogger Logger = LoggingConfig.Logger; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment