Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console LogExporter to display Body #3213

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* LogExporter to print Body from `LogRecord.FormattedMessage`
or from special casing `{OriginalFormat}` Key from
`LogRecord.StateValues`.
([#3213](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3213))

## 1.3.0-beta.1

Released 2022-Apr-15
Expand Down
21 changes: 19 additions & 2 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public override ExportResult Export(in Batch<LogRecord> batch)

this.WriteLine($"{"LogRecord.CategoryName:",-RightPaddingLength}{logRecord.CategoryName}");
this.WriteLine($"{"LogRecord.LogLevel:",-RightPaddingLength}{logRecord.LogLevel}");

string body = string.Empty;
if (logRecord.FormattedMessage != null)
{
this.WriteLine($"{"LogRecord.FormattedMessage:",-RightPaddingLength}{logRecord.FormattedMessage}");
body = logRecord.FormattedMessage;
}

if (logRecord.State != null)
Expand All @@ -58,10 +60,25 @@ public override ExportResult Export(in Batch<LogRecord> batch)
this.WriteLine("LogRecord.StateValues (Key:Value):");
for (int i = 0; i < logRecord.StateValues.Count; i++)
{
this.WriteLine($"{logRecord.StateValues[i].Key,-RightPaddingLength}{logRecord.StateValues[i].Value}");
// Special casing {OriginalFormat}
// See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182
// for explanation.
if (logRecord.StateValues[i].Key.Equals("{OriginalFormat}") && string.IsNullOrEmpty(body))
{
body = logRecord.StateValues[i].Value as string;
}
else
{
this.WriteLine($"{logRecord.StateValues[i].Key,-RightPaddingLength}{logRecord.StateValues[i].Value}");
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

if (!string.IsNullOrEmpty(body))
{
this.WriteLine($"{"Body:",-RightPaddingLength}{body}");
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}

if (logRecord.EventId != default)
{
this.WriteLine($"{"LogRecord.EventId:",-RightPaddingLength}{logRecord.EventId.Id}");
Expand Down