Skip to content

Commit

Permalink
.Net: Update TELEMETRY.md to include the latest recommended pattern f…
Browse files Browse the repository at this point in the history
…or ILoggerFactory (#6587)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Upon reviewing the Microsoft Learn documentation update, it has come to
our attention that the TELEMETRY.md file contains outdated
recommendations regarding the setup of `ILoggerFactory`.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Ensure the documentation contains the latest recommendations and
improve clarity.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
TaoChenOSU authored Jun 6, 2024
1 parent 546ec65 commit bebe127
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions dotnet/docs/TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Code example using Application Insights can be found [here](../samples/Demos/Tel

## Logging

The logging mechanism in this project relies on the `ILogger` interface from the `Microsoft.Extensions.Logging` namespace. Recent updates have introduced enhancements to the logger creation process. Instead of directly using the `ILogger` interface, instances of `ILogger` are now recommended to be created through an `ILoggerFactory` provided to components using the `WithLoggerFactory` method.
The logging mechanism in this project relies on the `ILogger` interface from the `Microsoft.Extensions.Logging` namespace. Recent updates have introduced enhancements to the logger creation process. Instead of directly using the `ILogger` interface, instances of `ILogger` are now recommended to be created through an `ILoggerFactory` configured through a `ServiceCollection`.

By employing the `WithLoggerFactory` approach, logger instances are generated with precise type information, facilitating more accurate logging and streamlined control over log filtering across various classes.
By employing the `ILoggerFactory` approach, logger instances are generated with precise type information, facilitating more accurate logging and streamlined control over log filtering across various classes.

Log levels used in SK:

Expand All @@ -36,7 +36,13 @@ Log levels used in SK:
Enable logging for Kernel instance:

```csharp
var kernel = new KernelBuilder().WithLoggerFactory(loggerFactory);
IKernelBuilder builder = Kernel.CreateBuilder();

// Assuming loggerFactory is already defined.
builder.Services.AddSingleton(loggerFactory);
...

var kernel = builder.Build();
```

All kernel functions and planners will be instrumented. It includes _logs_, _metering_ and _tracing_.
Expand All @@ -46,16 +52,19 @@ All kernel functions and planners will be instrumented. It includes _logs_, _met
Log filtering configuration has been refined to strike a balance between visibility and relevance:

```csharp
// Add OpenTelemetry as a logging provider
builder.AddOpenTelemetry(options =>
using var loggerFactory = LoggerFactory.Create(builder =>
{
options.AddAzureMonitorLogExporter(options => options.ConnectionString = connectionString);
// Format log messages. This is default to false.
options.IncludeFormattedMessage = true;
});
builder.AddFilter("Microsoft", LogLevel.Warning);
builder.AddFilter("Microsoft.SemanticKernel", LogLevel.Critical);
builder.AddFilter("Microsoft.SemanticKernel.Reliability", LogLevel.Information);
// Add OpenTelemetry as a logging provider
builder.AddOpenTelemetry(options =>
{
// Assuming connectionString is already defined.
options.AddAzureMonitorLogExporter(options => options.ConnectionString = connectionString);
// Format log messages. This is default to false.
options.IncludeFormattedMessage = true;
});
builder.AddFilter("Microsoft", LogLevel.Warning);
builder.AddFilter("Microsoft.SemanticKernel", LogLevel.Information);
}
```

> Read more at: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/customizing-the-sdk/README.md
Expand Down

0 comments on commit bebe127

Please sign in to comment.