Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll chrisfcarroll/LogAssert.cs
Last active Nov 29, 2018

Embed
What would you like to do?
Runtime Assertion, Log.Assert, TryLog, TryElse methods
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
You can’t perform that action at this time.