Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions aspnetcore/fundamentals/logging/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ Calling `CreateLogger` with a fixed name can be useful when used in multiple met

`ILogger<T>` is equivalent to calling `CreateLogger` with the fully qualified type name of `T`.

***NOTE:*** [LoggerFactory.Create](/dotnet/api/microsoft.extensions.logging.loggerfactory.create) Creates a new instance of <xref:Microsoft.Extensions.Logging.ILoggerFactory> that's configured using the provided `configure` delegate. The new instance of `ILoggerFactory` creates a new logging pipeline, different from the default logging pipeline. The new logging pipeline is not configured by the `Logging` section of `appsettings.json` or `appsettings.{ENVIRONMENT}.json`. For more information, see [Logging configuration](xref:fundamentals/logging/index#configure-logging).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noahfalk @cijothomas who can review this NOTE: and suggest more accurate wording. Much of it was written by the ChatGPT plugin for VS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text of the note seems quite accurate to me, but the note itself feels disconnected from the text around it. Assuming that we make the other changes in the snippets then this docs page will have no references to using LoggerFactory.Create and I think the note can be omitted. I am guessing a lot of the confusion was occurring because some sample snippets were using LoggerFactory.Create and people would copy those snippets.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that we make the other changes in the snippets then this docs page will have no references to using LoggerFactory.Create and I think the note can be omitted.

+1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Very impressed with ChatGPT's notes, btw!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rick-Anderson I suggest to remove this note. Its no longer needed, and having it there is probably confusing without any context.


<a name="llvl"></a>

## Log level
Expand Down Expand Up @@ -877,25 +879,12 @@ The following example shows how to register filter rules in code:

The logging libraries implicitly create a scope object with `SpanId`, `TraceId`, `ParentId`,`Baggage`, and `Tags`. This behavior is configured via <xref:Microsoft.Extensions.Logging.LoggerFactoryOptions.ActivityTrackingOptions>.

```csharp
var loggerFactory = LoggerFactory.Create(logging =>
{
logging.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId
| ActivityTrackingOptions.Baggage
| ActivityTrackingOptions.Tags;
}).AddSimpleConsole(options =>
{
options.IncludeScopes = true;
});
});
```
[!code-csharp[](~/fundamentals/logging/index/samples/6.x/WebApp/Program.cs?name=snippet4)]

If the `traceparent` http request header is set, the `ParentId` in the log scope shows the W3C `parent-id` from in-bound `traceparent` header and the `SpanId` in the log scope shows the updated `parent-id` for the next out-bound step/span. For more information, see [Mutating the traceparent Field](https://www.w3.org/TR/trace-context/#mutating-the-traceparent-field).

<!-- Add warning here once warning is approved -->

## Create a custom logger

To create a custom logger, see [Implement a custom logging provider in .NET](/dotnet/core/extensions/custom-logging-provider).
Expand Down
60 changes: 41 additions & 19 deletions aspnetcore/fundamentals/logging/index/samples/6.x/WebLog/Program.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,80 @@
#define APP3 // FIRST SECOND APP3
#define FIRST // FIRST SECOND APP3 APP4
#if NEVER
#elif FIRST
#region snippet1
// <snippet1>
var builder = WebApplication.CreateBuilder(args);

var logger = LoggerFactory.Create(config =>
{
config.AddConsole();
}).CreateLogger("Program");
// Configure logging with AddConsole()
builder.Logging.AddConsole();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.MapGet("/Test", async context =>
app.MapGet("/Test", async (ILogger logger, HttpResponse response) =>
{
logger.LogInformation("Testing logging in Program.cs");
await context.Response.WriteAsync("Testing");
await response.WriteAsync("Testing");
});

app.Run();
#endregion
// </snippet1>
#elif SECOND
#region snippet2
// <snippet2>
using Microsoft.Extensions.Logging.Console;

var builder = WebApplication.CreateBuilder(args);

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddSimpleConsole(i => i.ColorBehavior = LoggerColorBehavior.Disabled);
});

var logger = loggerFactory.CreateLogger<Program>();
// Configure logging with AddSimpleConsole
builder.Logging.AddSimpleConsole(i => i.ColorBehavior = LoggerColorBehavior.Disabled);

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.MapGet("/Test", async context =>
{
var logger = app.Logger; // Access the configured logger directly
logger.LogInformation("Testing logging in Program.cs");
await context.Response.WriteAsync("Testing");
});

app.Run();
#endregion

// </snippet2>
#elif APP3
#region snippet3
// <snippet3>
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Logger.LogInformation("Adding Routes");
app.MapGet("/", () => "Hello World!");
app.Logger.LogInformation("Starting the app");
app.Run();
#endregion
// </snippet3>
#elif APP4
// <snippet4>
var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
});

builder.Logging.Configure(options =>
{
options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId
| ActivityTrackingOptions.TraceId
| ActivityTrackingOptions.ParentId
| ActivityTrackingOptions.Baggage
| ActivityTrackingOptions.Tags;
});
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();
// </snippet4>
#elif APP5
// <snippet5>
// </snippet5>
#endif