/// <summary> /// Logs the and then ignores the exceptions thrown from the task if any. /// </summary> /// <param name="self">The task itself</param> /// <param name="logger">Logger to log against</param> /// <param name="msg">Optional message to be included</param> /// <param name="logLevel">Optional parameter to set log level</param> public static void LogAndIgnoreExceptionIfAny(this Task self, Logger logger, string msg = "", Level logLevel = Level.Error) { self.ContinueWith(t => { // ReSharper disable once PossibleNullReferenceException ; We know the task is Faulted logger.Log(logLevel, "{0} Exception:{1}", t.Exception.GetBaseException()); }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously); }
/// <summary> /// Call this method to throw an exception. /// </summary> /// <remarks> /// Calling this method will trace the exception and do other common processing, /// and then it will throw the exception. This method traces the exception type /// and message at error level and the full stack trace at all other levels. /// </remarks> /// <example> /// Exceptions.Throw(new Exception("Some exception")); /// </example> /// <param name="exception">The exception to be thrown.</param> /// <param name="message">The message from the caller class.</param> /// <param name="logger">The logger from the caller class.</param> public static void Throw(Exception exception, string message, Logger logger) { string logMessage = string.Concat(DiagnosticsMessages.ExceptionThrowing, " ", exception.GetType().Name, " ", message); if (logger == null) { Console.WriteLine("Exception caught before logger is initiated, error message: " + logMessage + exception.Message); } else { logger.Log(Level.Error, logMessage, exception); } throw exception; }
/// <summary> /// Initializes a new instance of the LoggingScope class. /// </summary> /// <param name="logger"></param> /// <param name="format"></param> /// <param name="args"></param> public LoggingScope(Logger logger, string format, params object[] args) { _logger = logger; _stopWatch = Stopwatch.StartNew(); string content = args.Length > 0 ? string.Format(CultureInfo.InvariantCulture, format, args) : format; _content = content; _logger.Log(Level.Start, content); }
public static void CaughtAndThrow(Exception exception, Level level, string message, Logger logger) { string logMessage = string.Concat(DiagnosticsMessages.ExceptionCaught, " ", exception.GetType().Name, " ", message); if (logger == null) { Console.WriteLine("Exception caught before logger is initiated, error message: " + logMessage + exception.Message); } else { logger.Log(level, logMessage, exception); } ExceptionDispatchInfo.Capture(exception).Throw(); }