forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
64 lines (50 loc) · 2.02 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using Microsoft.Extensions.Logging;
using OpenTelemetry;
namespace ExtendingTheSdk;
public class Program
{
public static void Main()
{
using var loggerFactory = LoggerFactory.Create(builder =>
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddProcessor(new MyProcessor("ProcessorA"))
.AddProcessor(new MyProcessor("ProcessorB"))
.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX")))
.AddMyExporter();
}));
var logger = loggerFactory.CreateLogger<Program>();
// unstructured log
logger.LogInformation("Hello, World!");
// String interpolation, as in the below line, results in unstructured logging, and is not recommended
// logger.LogInformation($"Hello from potato {0.99}.");
// structured log with template
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
// structured log with strong type
logger.LogInformation("{food}", new Food { Name = "artichoke", Price = 3.99 });
// structured log with anonymous type
logger.LogInformation("{food}", new { Name = "pumpkin", Price = 5.99 });
// structured log with general type
logger.LogInformation("{food}", new Dictionary<string, object>
{
["Name"] = "truffle",
["Price"] = 299.99,
});
// log with scopes
using (logger.BeginScope("[operation]"))
using (logger.BeginScope("[hardware]"))
{
logger.LogError("{name} is broken.", "refrigerator");
}
// message will be redacted by MyRedactionProcessor
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>");
}
internal struct Food
{
public string Name { get; set; }
public double Price { get; set; }
}
}