forked from signalfx/signalfx-dotnet-tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
70 lines (63 loc) · 3.37 KB
/
Program.cs
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
// Modified by SignalFx
using Serilog;
using Serilog.Context;
using Serilog.Formatting.Compact;
using Serilog.Formatting.Json;
using SignalFx.Tracing;
namespace SerilogExample
{
class Program
{
static void Main(string[] args)
{
// Regardless of the output layout, your LoggerConfiguration must be
// enriched from the LogContext to extract the `trace_id`, `span_id`, `service.name`
// and `deployment.environment`
// properties that are automatically injected by the .NET tracer
//
// Additions to LoggerConfiguration:
// - .Enrich.FromLogContext()
var loggerConfiguration = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Information);
// When using a message template, you must emit all properties using the {Properties} syntax in order to emit `trace_id` and `span_id` (see: https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text)
// This is because Serilog cannot look up these individual keys by name due to the '.' in the key name (see https://github.com/serilog/serilog/wiki/Writing-Log-Events#message-template-syntax)
// Additionally, Datadog will only parse log properties if they are in a JSON-like map, and the values for trace_id and span_id must be surrounded by quotes
//
// Additions to layout:
// - {Properties}
//
loggerConfiguration = loggerConfiguration
.WriteTo.File(
"log-Serilog-textFile.log",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Properties} {Message:lj} {NewLine}{Exception}");
// The built-in JsonFormatter will display all properties by default, so no extra work is needed to emit `trace_id` and `span_id`
//
// Additions to layout: none
//
loggerConfiguration = loggerConfiguration
.WriteTo.File(
new JsonFormatter(),
"log-Serilog-jsonFile-allProperties.log");
// The CompactJsonFormatter from the Serilog.Formatting.Compact NuGet package will display all properties by default, so no extra work is needed to emit `trace_id` and `span_id`
//
// Additions to layout: none
//
loggerConfiguration = loggerConfiguration
.WriteTo.File(
new CompactJsonFormatter(),
"log-Serilog-compactJsonFile-allProperties.log");
// Main procedure
var log = loggerConfiguration.CreateLogger();
using (LogContext.PushProperty("order-number", 1024))
{
log.Information("Message before a trace.");
using (var scope = Tracer.Instance.StartActive("SerilogExample - Main()"))
{
log.Information("Message during a trace.");
}
log.Information("Message after a trace.");
}
}
}
}