-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenTelemetryFormattedConsoleExporter.cs
More file actions
61 lines (52 loc) · 2.48 KB
/
OpenTelemetryFormattedConsoleExporter.cs
File metadata and controls
61 lines (52 loc) · 2.48 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
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using System;
using System.Net;
namespace MergerLogic.Monitoring
{
public class OpenTelemetryFormattedConsoleExporter : ConsoleExporter<LogRecord>
{
private const string SERVICE_NAME_ATTRIBUTE = "service.name";
private const string SERVICE_VERSION_ATTRIBUTE = "service.version";
private const string SERVICE_HOST_NAME_ATTRIBUTE = "service.host.name";
public OpenTelemetryFormattedConsoleExporter(ConsoleExporterOptions options) : base(options)
{
}
public override ExportResult Export(in Batch<LogRecord> batch)
{
foreach (var logRecord in batch)
{
string log = this.MCTextFormat(logRecord);
this.WriteLine(log);
}
return ExportResult.Success;
}
private string MCTextFormat(LogRecord record)
{
var resource = this.ParseResource();
var serviceName = this.GetResourceAttribute(resource, SERVICE_NAME_ATTRIBUTE, "unknown_service");
var serviceVersion = this.GetResourceAttribute(resource, SERVICE_VERSION_ATTRIBUTE, "unknown_version");
if (!resource.ContainsKey(SERVICE_HOST_NAME_ATTRIBUTE))
{
resource.Add(SERVICE_HOST_NAME_ATTRIBUTE, Dns.GetHostName());
}
var serviceHostName = this.GetResourceAttribute(resource, SERVICE_HOST_NAME_ATTRIBUTE, "unknown_host_name");
var exception = record.Exception != null ? $" [{record.Exception}]" : string.Empty;
return $"[{this.FormatTime(record.Timestamp)}] [{record.LogLevel}] [{serviceName}] [{serviceHostName}] [{serviceVersion}] [{Environment.CurrentManagedThreadId}] [{record.CategoryName}] {record.State}{exception}";
}
private string FormatTime(DateTime time)
{
return time.ToString("O", System.Globalization.CultureInfo.InvariantCulture);
}
private Dictionary<string, object> ParseResource()
{
var attributes = this.ParentProvider.GetResource()?.Attributes;
return attributes != null ? new Dictionary<string, object>(attributes) : new Dictionary<string, object>();
}
private string GetResourceAttribute(Dictionary<string, object> resource, string attribute, string defaultValue)
{
return resource.ContainsKey(attribute) ? resource[attribute]?.ToString() : defaultValue;
}
}
}