public static async Task <T> LogIfThrows <T>(this IFullLogger This, LogLevel level, string message, Func <Task <T> > block) { try { return(await block()); } catch (Exception ex) { switch (level) { case LogLevel.Debug: This.DebugException(message ?? "", ex); break; case LogLevel.Info: This.InfoException(message ?? "", ex); break; case LogLevel.Warn: This.WarnException(message ?? "", ex); break; case LogLevel.Error: This.ErrorException(message ?? "", ex); break; } throw; } }
public static void LogIfThrows(this IFullLogger This, LogLevel level, string message, Action block) { try { block(); } catch (Exception ex) { switch (level) { case LogLevel.Debug: This.DebugException(message ?? "", ex); break; case LogLevel.Info: This.InfoException(message ?? "", ex); break; case LogLevel.Warn: This.WarnException(message ?? "", ex); break; case LogLevel.Error: This.ErrorException(message ?? "", ex); break; } throw; } }
/// <summary> /// Sends the value provided by the provided delegate, only if Info is enabled. /// </summary> /// <param name="logger">The logger to use.</param> /// <param name="function">The function to evaluate if Info logging is enabled.</param> /// <param name="exception">A exception to log about.</param> public static void InfoException(this IFullLogger logger, Func <string> function, Exception exception) { if (logger.IsInfoEnabled) { #pragma warning disable CS0618 // Type or member is obsolete logger.InfoException(function.Invoke(), exception); #pragma warning restore CS0618 // Type or member is obsolete } }
/// <summary> /// Sends the value provided by the provided delegate, only if Info is enabled. /// </summary> /// <param name="logger">The logger to use.</param> /// <param name="function">The function to evaluate if Info logging is enabled.</param> /// <param name="exception">A exception to log about.</param> public static void InfoException(this IFullLogger logger, Func <string> function, Exception exception) { if (logger is null) { throw new ArgumentNullException(nameof(logger)); } if (function is null) { throw new ArgumentNullException(nameof(function)); } if (logger.IsInfoEnabled) { #pragma warning disable CS0618 // Type or member is obsolete logger.InfoException(function.Invoke(), exception); #pragma warning restore CS0618 // Type or member is obsolete } }