-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathConsoleLogRecordExporter.cs
More file actions
168 lines (140 loc) · 6.13 KB
/
Copy pathConsoleLogRecordExporter.cs
File metadata and controls
168 lines (140 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Globalization;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
namespace OpenTelemetry.Exporter;
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
{
private const int RightPaddingLength = 35;
private readonly Lock syncObject = new();
private bool disposed;
private string? disposedStackTrace;
private bool isDisposeMessageSent;
public ConsoleLogRecordExporter(ConsoleExporterOptions options)
: base(options)
{
}
public override ExportResult Export(in Batch<LogRecord> batch)
{
if (this.disposed)
{
if (!this.isDisposeMessageSent)
{
lock (this.syncObject)
{
if (this.isDisposeMessageSent)
{
return ExportResult.Failure;
}
this.isDisposeMessageSent = true;
}
this.WriteLine("The console exporter is still being invoked after it has been disposed. This could be due to the application's incorrect lifecycle management of the LoggerFactory/OpenTelemetry .NET SDK.");
this.WriteLine(Environment.StackTrace);
this.WriteLine($"{Environment.NewLine}Dispose was called on the following stack trace:");
this.WriteLine(this.disposedStackTrace!);
}
return ExportResult.Failure;
}
foreach (var logRecord in batch)
{
var timestamp = logRecord.Timestamp;
this.WriteLine($"{"LogRecord.Timestamp:",-RightPaddingLength}{(timestamp == DateTime.MinValue ? "(not set)" : timestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture))}");
if (logRecord.TraceId != default)
{
this.WriteLine($"{"LogRecord.TraceId:",-RightPaddingLength}{logRecord.TraceId}");
this.WriteLine($"{"LogRecord.SpanId:",-RightPaddingLength}{logRecord.SpanId}");
this.WriteLine($"{"LogRecord.TraceFlags:",-RightPaddingLength}{logRecord.TraceFlags}");
}
if (logRecord.CategoryName != null)
{
this.WriteLine($"{"LogRecord.CategoryName:",-RightPaddingLength}{logRecord.CategoryName}");
}
if (logRecord.Severity.HasValue)
{
this.WriteLine($"{"LogRecord.Severity:",-RightPaddingLength}{logRecord.Severity}");
}
if (logRecord.SeverityText != null)
{
this.WriteLine($"{"LogRecord.SeverityText:",-RightPaddingLength}{logRecord.SeverityText}");
}
if (logRecord.FormattedMessage != null)
{
this.WriteLine($"{"LogRecord.FormattedMessage:",-RightPaddingLength}{logRecord.FormattedMessage}");
}
if (logRecord.Body != null)
{
this.WriteLine($"{"LogRecord.Body:",-RightPaddingLength}{logRecord.Body}");
}
if (logRecord.Attributes != null)
{
this.WriteLine("LogRecord.Attributes (Key:Value):");
for (var i = 0; i < logRecord.Attributes.Count; i++)
{
// Special casing {OriginalFormat}
// See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182
// for explanation.
var valueToTransform = string.Equals(logRecord.Attributes[i].Key, "{OriginalFormat}", StringComparison.Ordinal)
? new KeyValuePair<string, object?>("OriginalFormat (a.k.a Body)", logRecord.Attributes[i].Value)
: logRecord.Attributes[i];
if (this.TagWriter.TryTransformTag(valueToTransform, out var result))
{
this.WriteLine($"{string.Empty,-4}{result.Key}: {result.Value}");
}
}
}
if (logRecord.EventId != default)
{
this.WriteLine($"{"LogRecord.EventId:",-RightPaddingLength}{logRecord.EventId.Id}");
if (!string.IsNullOrEmpty(logRecord.EventId.Name))
{
this.WriteLine($"{"LogRecord.EventName:",-RightPaddingLength}{logRecord.EventId.Name}");
}
}
if (logRecord.Exception != null)
{
this.WriteLine($"{"LogRecord.Exception:",-RightPaddingLength}{logRecord.Exception.ToInvariantString()}");
}
var scopeDepth = -1;
logRecord.ForEachScope(ProcessScope, this);
void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
{
if (++scopeDepth == 0)
{
exporter.WriteLine("LogRecord.ScopeValues (Key:Value):");
}
foreach (var scopeItem in scope)
{
if (this.TagWriter.TryTransformTag(scopeItem, out var result))
{
exporter.WriteLine($"[Scope.{scopeDepth}]:{result.Key}: {result.Value}");
}
}
}
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine($"{Environment.NewLine}Resource associated with LogRecord:");
foreach (var resourceAttribute in resource.Attributes)
{
if (this.TagWriter.TryTransformTag(resourceAttribute.Key, resourceAttribute.Value, out var result))
{
this.WriteLine($"{result.Key}: {result.Value}");
}
}
}
this.WriteLine(string.Empty);
}
return ExportResult.Success;
}
protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
this.disposed = true;
this.disposedStackTrace = Environment.StackTrace;
}
base.Dispose(disposing);
}
}