Skip to content

Commit

Permalink
print out exception.data
Browse files Browse the repository at this point in the history
  • Loading branch information
WernerMairl committed Jul 24, 2020
1 parent 2732907 commit dbe8652
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ public ConsoleFormatterOptions() { }
/// Gets or sets indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to <c>false</c>.
/// </summary>
public bool UseUtcTimestamp { get; set; }

/// <summary>
/// Includes key/values from Exception.Data when <see langword="true" />.
/// </summary>
public bool IncludeExceptionDataDictionary { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using System.Collections;

namespace Microsoft.Extensions.Logging.Console
{
Expand Down Expand Up @@ -70,6 +71,7 @@ public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeP
private void CreateDefaultLogMessage<TState>(TextWriter textWriter, in LogEntry<TState> logEntry, string message, IExternalScopeProvider scopeProvider)
{
bool singleLine = FormatterOptions.SingleLine;
bool includeExceptionData = FormatterOptions.IncludeExceptionDataDictionary;
int eventId = logEntry.EventId.Id;
Exception exception = logEntry.Exception;

Expand All @@ -91,10 +93,24 @@ private void CreateDefaultLogMessage<TState>(TextWriter textWriter, in LogEntry<
// Example:
// System.InvalidOperationException
// at Namespace.Class.Function() in File:line X

if (exception != null)
{
// exception message
WriteMessage(textWriter, exception.ToString(), singleLine);
if (includeExceptionData && (exception.Data != null) && exception.Data.Count > 0)
{
List<string> lines = new List<string>(exception.Data.Count);
foreach (DictionaryEntry entry in exception.Data)
{
lines.Add(string.Format("{0}={1}", entry.Key, entry.Value));
}
string separator = singleLine ? string.Empty : Environment.NewLine;
WriteMessage(textWriter, exception.ToString() + separator + string.Join(separator, lines), singleLine);
}
else
{
WriteMessage(textWriter, exception.ToString(), singleLine);
}
}
if (singleLine)
{
Expand Down

0 comments on commit dbe8652

Please sign in to comment.