-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugActivityExporter.cs
More file actions
53 lines (45 loc) · 1.62 KB
/
DebugActivityExporter.cs
File metadata and controls
53 lines (45 loc) · 1.62 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
using System.Diagnostics;
using System.Text;
using OpenTelemetry;
namespace CodingWithCalvin.Otel4Vsix.Exporters;
/// <summary>
/// An activity exporter that writes trace data to <see cref="System.Diagnostics.Debug"/>.
/// Output appears in the Visual Studio Output window when debugging.
/// </summary>
internal sealed class DebugActivityExporter : BaseExporter<Activity>
{
private readonly string _prefix;
public DebugActivityExporter(string serviceName, string serviceVersion)
{
_prefix = $"[{serviceName} v{serviceVersion}]";
}
/// <inheritdoc />
public override ExportResult Export(in Batch<Activity> batch)
{
foreach (var activity in batch)
{
var sb = new StringBuilder();
sb.AppendLine($"{_prefix} [TRACE] {activity.DisplayName}");
sb.AppendLine($" TraceId: {activity.TraceId}");
sb.AppendLine($" SpanId: {activity.SpanId}");
sb.AppendLine($" Duration: {activity.Duration.TotalMilliseconds:F2}ms");
sb.AppendLine($" Status: {activity.Status}");
if (activity.Tags != null)
{
foreach (var tag in activity.Tags)
{
sb.AppendLine($" {tag.Key}: {tag.Value}");
}
}
if (activity.Events != null)
{
foreach (var evt in activity.Events)
{
sb.AppendLine($" Event: {evt.Name} at {evt.Timestamp}");
}
}
System.Diagnostics.Trace.WriteLine(sb.ToString());
}
return ExportResult.Success;
}
}