示例#1
0
 /// <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);
 }
示例#2
0
        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);
        }
示例#3
0
        /// <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());
        }
示例#4
0
 /// <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);
 }
示例#5
0
 /// <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);
 }
示例#6
0
        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;
            }
        }
示例#7
0
 /// <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);
 }
示例#8
0
 /// <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);
 }
示例#9
0
 /// <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);
 }
示例#10
0
        //------------------------------------------HELPERS------------------------------------------//

        private static void Log(
            this ILogger logger,
            LogLevel logLevel,
            ILogValues state,
            Exception exception = null)
        {
            logger.Log(logLevel, 0, state, exception, _messageFormatter);
        }
示例#11
0
 /// <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);
 }
示例#12
0
 /// <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);
 }
示例#13
0
 /// <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);
 }
示例#14
0
        private static string LogValuesFormatter(ILogValues state, Exception exception)
        {
            if (exception == null)
            {
                return(state.Format());
            }

            return(state.Format() + Environment.NewLine + exception);
        }
示例#15
0
 private static void LogWithEvent(
     this ILogger logger,
     LogLevel logLevel,
     int eventId,
     ILogValues state,
     Exception exception = null)
 {
     logger.Log(logLevel, eventId, state, exception, _messageFormatter);
 }
示例#16
0
        /// <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();
        }
示例#17
0
        /// <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;
            }
        }
示例#18
0
        /// <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;
            }
        }
示例#19
0
        /// <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());
        }
示例#20
0
        /// <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);
        }
示例#21
0
        /// <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);
        }
示例#22
0
 private static void LogWithEvent(
     this ILogger logger,
     LogLevel logLevel,
     int eventId,
     ILogValues state,
     Exception exception = null)
 {
     logger.Log(logLevel, eventId, state, exception, _logValuesFormatter);
 }
示例#23
0
        private static string LogValuesFormatter(ILogValues state, Exception exception)
        {
            if (exception == null)
            {
                return state.Format();
            }

            return state.Format() + Environment.NewLine + exception;
        }
示例#24
0
 //------------------------------------------HELPERS------------------------------------------//
 private static void Log(
     this ILogger logger,
     LogLevel logLevel,
     ILogValues state,
     Exception exception = null)
 {
     logger.Log(logLevel, 0, state, exception, _logValuesFormatter);
 }
示例#25
0
 /// <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);
 }
示例#26
0
 /// <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);
 }
示例#27
0
 /// <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);
 }
示例#28
0
 /// <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);
 }
示例#29
0
 /// <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);
 }
示例#30
0
        /// <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);
        }
 /// <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);
 }
 /// <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);
 }
示例#33
0
        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;
            }
        }
示例#34
0
        /// <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);
        }
 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;
     }
 }
示例#36
0
 public StructureEnricher(ILogValues structure)
 {
     _structure = structure;
 }