-
Notifications
You must be signed in to change notification settings - Fork 773
/
Program.cs
56 lines (47 loc) · 1.83 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
{
logging.IncludeScopes = true;
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
});
});
var logger = loggerFactory.CreateLogger<Program>();
logger.FoodPriceChanged("artichoke", 9.99);
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("store", "Seattle"),
}))
{
logger.FoodPriceChanged("truffle", 999.99);
}
logger.FoodRecallNotice(
brandName: "Contoso",
productDescription: "Salads",
productType: "Food & Beverages",
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");
// Dispose logger factory before the application ends.
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
[LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")]
public static partial void FoodRecallNotice(
this ILogger logger,
string brandName,
string productDescription,
string productType,
string recallReasonDescription,
string companyName);
}