forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
49 lines (36 loc) · 1.55 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
var builder = WebApplication.CreateBuilder(args);
// Remove default providers and add OpenTelemetry logging provider.
// For instructional purposes only, disable the default .NET console logging provider to
// use the verbose OpenTelemetry console exporter instead. For most development
// and production scenarios the default console provider works well and there is no need to
// clear these providers.
builder.Logging.ClearProviders();
builder.Logging.AddOpenTelemetry(logging =>
{
var resourceBuilder = ResourceBuilder
.CreateDefault()
.AddService(builder.Environment.ApplicationName);
logging.SetResourceBuilder(resourceBuilder)
// ConsoleExporter is used for demo purpose only.
// In production environment, ConsoleExporter should be replaced with other exporters (e.g. OTLP Exporter).
.AddConsoleExporter();
});
var app = builder.Build();
app.MapGet("/", (ILogger<Program> logger) =>
{
logger.FoodPriceChanged("artichoke", 9.99);
return "Hello from OpenTelemetry Logs!";
});
app.Logger.StartingApp();
app.Run();
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Starting the app...")]
public static partial void StartingApp(this ILogger logger);
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
}