Skip to content

Commit 3fb2ec5

Browse files
reyangCodeBlanch
andauthored
Log correlation/redaction doc improvements (#5262)
Co-authored-by: Mikel Blanchard <[email protected]>
1 parent eaf8316 commit 3fb2ec5

File tree

9 files changed

+77
-44
lines changed

9 files changed

+77
-44
lines changed

docs/logs/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,23 @@ instances if they are created by you.
153153
category name. Refer to the [.NET official
154154
document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category)
155155
to learn more.
156+
157+
## Log Correlation
158+
159+
In OpenTelemetry, logs are automatically correlated to traces. Check the [Log
160+
Correlation](./correlation/README.md) tutorial to learn more.
161+
162+
## Log Enrichment
163+
164+
TBD
165+
166+
## Log Filtering
167+
168+
Check the [Customizing OpenTelemetry .NET SDK for
169+
Logs](./customizing-the-sdk/README.md#log-filtering) document to learn more.
170+
171+
## Log Redaction
172+
173+
Logs might contain sensitive information such as passwords and credit card
174+
numbers, proper redaction is required to prevent privacy and security incidents.
175+
Check the [Log Redaction](./redaction/README.md) tutorial to learn more.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
internal static partial class LoggerExtensions
7+
{
8+
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
9+
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
10+
}

docs/logs/correlation/Program.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,39 @@
77
using OpenTelemetry.Logs;
88
using OpenTelemetry.Trace;
99

10-
namespace Correlation;
11-
1210
public class Program
1311
{
14-
private static readonly ActivitySource MyActivitySource = new(
15-
"MyCompany.MyProduct.MyLibrary");
12+
private static readonly ActivitySource MyActivitySource = new("MyCompany.MyProduct.MyLibrary");
1613

1714
public static void Main()
1815
{
19-
// Setup Logging
20-
using var loggerFactory = LoggerFactory.Create(builder =>
16+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
17+
.AddSource("MyCompany.MyProduct.MyLibrary")
18+
.AddConsoleExporter()
19+
.Build();
20+
21+
var loggerFactory = LoggerFactory.Create(builder =>
2122
{
22-
builder.AddOpenTelemetry(options =>
23+
builder.AddOpenTelemetry(logging =>
2324
{
24-
options.AddConsoleExporter();
25+
logging.AddConsoleExporter();
2526
});
2627
});
2728

2829
var logger = loggerFactory.CreateLogger<Program>();
2930

30-
// Setup Traces
31-
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
32-
.AddSource("MyCompany.MyProduct.MyLibrary")
33-
.AddConsoleExporter()
34-
.Build();
35-
36-
// Emit activity
3731
using (var activity = MyActivitySource.StartActivity("SayHello"))
3832
{
39-
activity?.SetTag("foo", 1);
40-
41-
// Emit logs within the context of activity
42-
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
33+
// Write a log within the context of an activity
34+
logger.FoodPriceChanged("artichoke", 9.99);
4335
}
36+
37+
// Dispose logger factory before the application ends.
38+
// This will flush the remaining logs and shutdown the logging pipeline.
39+
loggerFactory.Dispose();
40+
41+
// Dispose tracer provider before the application ends.
42+
// This will flush the remaining spans and shutdown the tracing pipeline.
43+
tracerProvider.Dispose();
4444
}
4545
}

docs/logs/correlation/README.md

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Logs correlation
1+
# Log Correlation
22

33
The getting started docs for [logs](../getting-started-console/README.md) and
44
[traces](../../trace/getting-started-console/README.md) showed how to emit logs
@@ -27,29 +27,32 @@ of an active `Activity`. Running the application will show the following output
2727
on the console:
2828

2929
```text
30-
LogRecord.Timestamp: 2022-05-18T18:51:16.4348626Z
31-
LogRecord.TraceId: d7aca5b2422ed8d15f56b6a93be4537d
32-
LogRecord.SpanId: c90ac2ad41ab4d46
30+
LogRecord.Timestamp: 2024-01-26T17:55:39.2273475Z
31+
LogRecord.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5
32+
LogRecord.SpanId: bd44308753200c58
3333
LogRecord.TraceFlags: Recorded
34-
LogRecord.CategoryName: Correlation.Program
35-
LogRecord.LogLevel: Information
36-
LogRecord.State: Hello from tomato 2.99.
34+
LogRecord.CategoryName: Program
35+
LogRecord.Severity: Info
36+
LogRecord.SeverityText: Information
37+
LogRecord.Body: Food `{name}` price changed to `{price}`.
38+
LogRecord.Attributes (Key:Value):
39+
name: artichoke
40+
price: 9.99
41+
OriginalFormat (a.k.a Body): Food `{name}` price changed to `{price}`.
42+
LogRecord.EventId: 344095174
43+
LogRecord.EventName: FoodPriceChanged
3744
38-
Resource associated with LogRecord:
39-
service.name: unknown_service:correlation
45+
...
4046
41-
Activity.TraceId: d7aca5b2422ed8d15f56b6a93be4537d
42-
Activity.SpanId: c90ac2ad41ab4d46
43-
Activity.TraceFlags: Recorded
47+
Activity.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5
48+
Activity.SpanId: bd44308753200c58
49+
Activity.TraceFlags: Recorded
4450
Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary
45-
Activity.DisplayName: SayHello
46-
Activity.Kind: Internal
47-
Activity.StartTime: 2022-05-18T18:51:16.3427411Z
48-
Activity.Duration: 00:00:00.2248932
49-
Activity.Tags:
50-
foo: 1
51-
Resource associated with Activity:
52-
service.name: unknown_service:correlation
51+
Activity.DisplayName: SayHello
52+
Activity.Kind: Internal
53+
Activity.StartTime: 2024-01-26T17:55:39.2223849Z
54+
Activity.Duration: 00:00:00.0361682
55+
...
5356
```
5457

5558
As you can see, the `LogRecord` automatically had the `TraceId`, `SpanId` fields

docs/logs/getting-started-aspnetcore/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
app.Run();
4141

42-
public static partial class ApplicationLogs
42+
internal static partial class LoggerExtensions
4343
{
4444
[LoggerMessage(LogLevel.Information, "Starting the app...")]
4545
public static partial void StartingApp(this ILogger logger);

docs/logs/getting-started-aspnetcore/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ has been used across the example, which delivers high performance, structured
105105
logging, and type-checked parameters:
106106

107107
```csharp
108-
public static partial class ApplicationLogs
108+
internal static partial class LoggerExtensions
109109
{
110110
[LoggerMessage(LogLevel.Information, "Starting the app...")]
111111
public static partial void StartingApp(this ILogger logger);

docs/logs/getting-started-console/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// This will flush the remaining logs and shutdown the logging pipeline.
2828
loggerFactory.Dispose();
2929

30-
public static partial class ApplicationLogs
30+
internal static partial class LoggerExtensions
3131
{
3232
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
3333
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);

docs/logs/getting-started-console/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ has been used across the example, which delivers high performance, structured
8888
logging, and type-checked parameters:
8989

9090
```csharp
91-
public static partial class ApplicationLogs
91+
internal static partial class LoggerExtensions
9292
{
9393
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
9494
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);

docs/logs/redaction/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// This will flush the remaining logs and shutdown the logging pipeline.
2323
loggerFactory.Dispose();
2424

25-
public static partial class ApplicationLogs
25+
internal static partial class LoggerExtensions
2626
{
2727
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
2828
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);

0 commit comments

Comments
 (0)