/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a critical log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogCritical( [NotNull] this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Critical, state, error); }
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func <object, Exception, string> formatter) { string message = string.Empty; ILogValues values = state as ILogValues; if (formatter != null) { message = formatter(state, exception); } //else if (values != null) //{ // message = $"{ logLevel.ToString() } ({eventId}): {exception?.ToString()}"; // if (exception != null) // { // message += Environment.NewLine + exception; // } //} else { message = LogFormatter.Formatter(state, exception); } LogDataQueue.Enqueue(message); }
/// <summary> /// Formats an <see cref="ILogValues"/>. /// </summary> /// <param name="logValues">The <see cref="ILogValues"/> to format.</param> /// <returns>A string representation of the given <see cref="ILogValues"/>.</returns> public static string FormatLogValues([NotNull] ILogValues logValues) { var builder = new StringBuilder(); FormatLogValues(logValues, builder); return(builder.ToString()); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a debug log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogDebug( [NotNull] this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Debug, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes an informational log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogInformation( [NotNull] this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Information, state, error); }
private void FormatLogValues(StringBuilder builder, ILogValues logValues, int level, bool bullet) { var values = logValues.GetValues(); if (values == null) { return; } var isFirst = true; foreach (var kvp in values) { builder.AppendLine(); if (bullet && isFirst) { builder.Append(' ', level * _indentation - 1) .Append('-'); } else { builder.Append(' ', level * _indentation); } builder.Append(kvp.Key) .Append(": "); if (kvp.Value is IEnumerable && !(kvp.Value is string)) { foreach (var value in (IEnumerable)kvp.Value) { if (value is ILogValues) { FormatLogValues( builder, (ILogValues)value, level + 1, bullet: true); } else { builder.AppendLine() .Append(' ', (level + 1) * _indentation) .Append(value); } } } else if (kvp.Value is ILogValues) { FormatLogValues( builder, (ILogValues)kvp.Value, level + 1, bullet: false); } else { builder.Append(kvp.Value); } isFirst = false; } }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a verbose log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogVerbose( [NotNull] this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Verbose, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes an error log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogError( this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Error, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a warning log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogWarning( this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Warning, state, error); }
//------------------------------------------HELPERS------------------------------------------// private static void Log( this ILogger logger, LogLevel logLevel, ILogValues state, Exception exception = null) { logger.Log(logLevel, 0, state, exception, _messageFormatter); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a debug log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogDebug( [NotNull] this ILogger logger, int eventId, ILogValues state, Exception error = null) { logger.LogWithEvent(LogLevel.Debug, eventId, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a critical log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogCritical( this ILogger logger, int eventId, ILogValues state, Exception error = null) { logger.LogWithEvent(LogLevel.Critical, eventId, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes an informational log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogInformation( this ILogger logger, int eventId, ILogValues state, Exception error = null) { logger.LogWithEvent(LogLevel.Information, eventId, state, error); }
private static string LogValuesFormatter(ILogValues state, Exception exception) { if (exception == null) { return(state.Format()); } return(state.Format() + Environment.NewLine + exception); }
private static void LogWithEvent( this ILogger logger, LogLevel logLevel, int eventId, ILogValues state, Exception exception = null) { logger.Log(logLevel, eventId, state, exception, _messageFormatter); }
/// <summary> /// Formats an <see cref="ILogValues"/>. /// </summary> /// <param name="logValues">The <see cref="ILogValues"/> to format.</param> /// <returns>A string representation of the given <see cref="ILogValues"/>.</returns> public static string FormatLogValues(ILogValues logValues) { if (logValues == null) { throw new ArgumentNullException(nameof(logValues)); } var builder = new StringBuilder(); FormatLogValues(logValues, builder); return builder.ToString(); }
/// <summary> /// Formats an <see cref="ILogValues"/>. /// </summary> /// <param name="logValues">The <see cref="ILogValues"/> to format.</param> /// <param name="builder">The <see cref="StringBuilder"/> to append to.</param> private static void FormatLogValues(ILogValues logValues, StringBuilder builder) { if (logValues == null) { throw new ArgumentNullException(nameof(logValues)); } if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var values = logValues.GetValues(); if (values == null) { return; } foreach (var kvp in values) { IEnumerable <ILogValues> structureEnumerable; ILogValues logs; builder.Append(kvp.Key); builder.Append(": "); if ((structureEnumerable = kvp.Value as IEnumerable <ILogValues>) != null) { var valArray = structureEnumerable.ToArray(); for (int j = 0; j < valArray.Length - 1; j++) { FormatLogValues(valArray[j], builder); builder.Append(", "); } if (valArray.Length > 0) { FormatLogValues(valArray[valArray.Length - 1], builder); } } else if ((logs = kvp.Value as ILogValues) != null) { FormatLogValues(logs, builder); } else { builder.Append(kvp.Value); } builder.Append(space); } // get rid of the extra whitespace if (builder.Length > 0) { builder.Length -= space.Length; } }
/// <summary> /// Formats an <see cref="ILogValues"/>. /// </summary> /// <param name="logValues">The <see cref="ILogValues"/> to format.</param> /// <param name="builder">The <see cref="StringBuilder"/> to append to.</param> private static void FormatLogValues(ILogValues logValues, StringBuilder builder) { if (logValues == null) { throw new ArgumentNullException(nameof(logValues)); } if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var values = logValues.GetValues(); if (values == null) { return; } foreach (var kvp in values) { IEnumerable<ILogValues> structureEnumerable; ILogValues logs; builder.Append(kvp.Key); builder.Append(": "); if ((structureEnumerable = kvp.Value as IEnumerable<ILogValues>) != null) { var valArray = structureEnumerable.ToArray(); for (int j = 0; j < valArray.Length - 1; j++) { FormatLogValues(valArray[j], builder); builder.Append(", "); } if (valArray.Length > 0) { FormatLogValues(valArray[valArray.Length - 1], builder); } } else if ((logs = kvp.Value as ILogValues) != null) { FormatLogValues(logs, builder); } else { builder.Append(kvp.Value); } builder.Append(space); } // get rid of the extra whitespace if (builder.Length > 0) { builder.Length -= space.Length; } }
/// <summary> /// Formats an <see cref="ILogValues"/>. /// </summary> /// <param name="logValues">The <see cref="ILogValues"/> to format.</param> /// <returns>A string representation of the given <see cref="ILogValues"/>.</returns> public static string FormatLogValues(ILogValues logValues) { if (logValues == null) { throw new ArgumentNullException(nameof(logValues)); } var builder = new StringBuilder(); FormatLogValues(logValues, builder); return(builder.ToString()); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a debug log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogDebug( this ILogger logger, ILogValues state, Exception error = null) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.Log(LogLevel.Debug, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a critical log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogCritical( this ILogger logger, int eventId, ILogValues state, Exception error = null) { if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.LogWithEvent(LogLevel.Critical, eventId, state, error); }
private static void LogWithEvent( this ILogger logger, LogLevel logLevel, int eventId, ILogValues state, Exception exception = null) { logger.Log(logLevel, eventId, state, exception, _logValuesFormatter); }
private static string LogValuesFormatter(ILogValues state, Exception exception) { if (exception == null) { return state.Format(); } return state.Format() + Environment.NewLine + exception; }
//------------------------------------------HELPERS------------------------------------------// private static void Log( this ILogger logger, LogLevel logLevel, ILogValues state, Exception exception = null) { logger.Log(logLevel, 0, state, exception, _logValuesFormatter); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a warning log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogWarning( [NotNull] this ILogger logger, int eventId, ILogValues state, Exception error = null) { logger.LogWithEvent(LogLevel.Warning, eventId, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a warning log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogWarning( [NotNull] this ILogger logger, ILogValues state, Exception error = null) { logger.Log(LogLevel.Warning, state, error); }
/// <summary> /// Formats the given <see cref="ILogValues"/> and writes a verbose log message. /// </summary> /// <param name="logger">The <see cref="ILogger"/> to write to.</param> /// <param name="eventId">The event id associated with the log.</param> /// <param name="state">The <see cref="ILogValues"/> to write.</param> /// <param name="error">The exception to log.</param> public static void LogVerbose( this ILogger logger, int eventId, ILogValues state, Exception error = null) { logger.LogWithEvent(LogLevel.Verbose, eventId, state, error); }
private void FormatLogValues(StringBuilder builder, ILogValues logValues, int level, bool bullet) { var values = logValues.GetValues(); if (values == null) { return; } var isFirst = true; foreach (var kvp in values) { builder.AppendLine(); if (bullet && isFirst) { builder.Append(' ', level * Indentation - 1) .Append('-'); } else { builder.Append(' ', level * Indentation); } builder.Append(kvp.Key) .Append(": "); var enumerable = kvp.Value as IEnumerable; if (enumerable != null && !(kvp.Value is string)) { foreach (var value in enumerable) { var vs = value as ILogValues; if (vs != null) { FormatLogValues( builder, vs, level + 1, true); } else { builder.AppendLine() .Append(' ', (level + 1) * Indentation) .Append(value); } } } else { var vs = kvp.Value as ILogValues; if (vs != null) { FormatLogValues( builder, vs, level + 1, false); } else { builder.Append(kvp.Value); } } isFirst = false; } }
public StructureEnricher(ILogValues structure) { _structure = structure; }