Esempio n. 1
0
        /// <inheritdoc/>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            bool exceptionOnly = logEvent.Exception != null && WithException && logEvent.Parameters?.Length == 1 && ReferenceEquals(logEvent.Parameters[0], logEvent.Exception) && logEvent.Message == "{0}";

            if (Raw)
            {
                builder.Append(logEvent.Message);
            }
            else if (!exceptionOnly)
            {
                if (ReferenceEquals(logEvent.MessageFormatter, LogMessageTemplateFormatter.DefaultAutoSingleTarget.MessageFormatter))
                {
                    // Skip string-allocation of LogEventInfo.FormattedMessage, but just write directly to StringBuilder
                    logEvent.AppendFormattedMessage(LogMessageTemplateFormatter.DefaultAutoSingleTarget, builder);
                }
                else
                {
                    builder.Append(logEvent.FormattedMessage);
                }
            }

            if (WithException && logEvent.Exception != null)
            {
                var primaryException = logEvent.Exception;
#if !NET3_5 && !SILVERLIGHT4
                if (logEvent.Exception is AggregateException aggregateException)
                {
                    aggregateException = aggregateException.Flatten();
                    primaryException   = aggregateException.InnerExceptions.Count == 1 ? aggregateException.InnerExceptions[0] : aggregateException;
                }
#endif
                if (!exceptionOnly)
                {
                    builder.Append(ExceptionSeparator);
                }
                builder.Append(primaryException.ToString());
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            bool exceptionOnly = logEvent.Exception != null && WithException && logEvent.Parameters?.Length == 1 && ReferenceEquals(logEvent.Parameters[0], logEvent.Exception) && logEvent.Message == "{0}";

            if (Raw)
            {
                builder.Append(logEvent.Message);
            }
            else if (!exceptionOnly)
            {
                if (logEvent.MessageFormatter?.Target is ILogMessageFormatter messageFormatter)
                {
                    logEvent.AppendFormattedMessage(messageFormatter, builder);
                }
                else
                {
                    builder.Append(logEvent.FormattedMessage);
                }
            }

            if (WithException && logEvent.Exception != null)
            {
                var primaryException = logEvent.Exception;
#if !NET35
                if (logEvent.Exception is AggregateException aggregateException)
                {
                    aggregateException = aggregateException.Flatten();
                    primaryException   = aggregateException.InnerExceptions.Count == 1 ? aggregateException.InnerExceptions[0] : aggregateException;
                }
#endif
                if (!exceptionOnly)
                {
                    builder.Append(ExceptionSeparator);
                }
                builder.Append(primaryException.ToString());
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Renders the log message including any positional parameters and appends it to the specified <see cref="StringBuilder" />.
 /// </summary>
 /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
 /// <param name="logEvent">Logging event.</param>
 protected override void Append(StringBuilder builder, LogEventInfo logEvent)
 {
     if (Raw)
     {
         builder.Append(logEvent.Message);
     }
     else
     {
         if (ReferenceEquals(logEvent.MessageFormatter, LogEventInfo.DefaultMessageFormatterSingleTarget) && (logEvent.MessageFormatter.Target is ILogMessageFormatter messageFormatter))
         {
             // Skip string-allocation of LogEventInfo.FormattedMessage, but just write directly to StringBuilder
             logEvent.AppendFormattedMessage(messageFormatter, builder);
         }
         else
         {
             builder.Append(logEvent.FormattedMessage);
         }
     }
     if (WithException && logEvent.Exception != null)
     {
         builder.Append(ExceptionSeparator);
         builder.Append(logEvent.Exception.ToString());
     }
 }