-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathLoggerExtensions.cs
More file actions
39 lines (34 loc) · 1.62 KB
/
Copy pathLoggerExtensions.cs
File metadata and controls
39 lines (34 loc) · 1.62 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Logging;
namespace OpenTelemetryDemo
{
/// <summary>
/// High-performance, source-generator-style logging (<see cref="LoggerMessage.Define"/>) for
/// this app's custom events. Each event's message template includes the
/// "microsoft.custom_event.name" placeholder, which becomes a `LogRecord` attribute that the
/// Azure Monitor OpenTelemetry Exporter (if configured) uses to classify the log as a custom
/// event in Application Insights, rather than a plain trace.
/// </summary>
public static class LoggerExtensions
{
private const string AzureMonitorCustomEventNameKey = "microsoft.custom_event.name";
private static readonly Action<ILogger, string, int, Exception> _vote = LoggerMessage.Define<string, int>(
LogLevel.Information,
new EventId(1, AzureMonitorCustomEventNameKey),
"{" + AzureMonitorCustomEventNameKey + "} {ImageRating}");
private static readonly Action<ILogger, string, string, long, Exception> _checkout = LoggerMessage.Define<string, string, long>(
LogLevel.Information,
new EventId(2, AzureMonitorCustomEventNameKey),
"{" + AzureMonitorCustomEventNameKey + "} {success} {checkoutAmount}");
public static void LogVote(this ILogger logger, int rating)
{
_vote(logger, "Vote", rating, null);
}
public static void LogCheckout(this ILogger logger, long amount)
{
_checkout(logger, "checkout", "yes", amount, null);
}
}
}