From 25086b4c7753691234648b912e5d7f7ba7be5c36 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Mon, 4 Mar 2024 10:54:42 -0800 Subject: [PATCH] Refine the log category convention (#31963) --- aspnetcore/fundamentals/logging/index.md | 4 ++-- .../index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aspnetcore/fundamentals/logging/index.md b/aspnetcore/fundamentals/logging/index.md index 2f4eafbaba..01c7fa3492 100644 --- a/aspnetcore/fundamentals/logging/index.md +++ b/aspnetcore/fundamentals/logging/index.md @@ -222,11 +222,11 @@ Logs that begin with "Microsoft" categories are from ASP.NET Core framework code ## Log category -When an `ILogger` object is created, a *category* is specified. That category is included with each log message created by that instance of `ILogger`. The category string is arbitrary, but the convention is to use the class name. For example, in a controller the name might be `"TodoApi.Controllers.TodoController"`. The ASP.NET Core web apps use `ILogger` to automatically get an `ILogger` instance that uses the fully qualified type name of `T` as the category: +When an `ILogger` object is created, a *category* is specified. That category is included with each log message created by that instance of `ILogger`. The category string is arbitrary, but the convention is to use the fully qualified class name. For example, in a controller the name might be `"TodoApi.Controllers.TodoController"`. The ASP.NET Core web apps use `ILogger` to automatically get an `ILogger` instance that uses the fully qualified type name of `T` as the category: [!code-csharp[](~/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Privacy.cshtml.cs?name=snippet)] -To explicitly specify the category, call `ILoggerFactory.CreateLogger`: +If further categorization is desired, the convention is to use a hierarchical name by appending a subcategory to the fully qualified class name, and explicitly specify the category using `ILoggerFactory.CreateLogger`: [!code-csharp[](~/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs?name=snippet)] diff --git a/aspnetcore/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs b/aspnetcore/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs index 716b5545d3..8e73bf0970 100644 --- a/aspnetcore/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs +++ b/aspnetcore/fundamentals/logging/index/samples/3.x/TodoApiDTO/Pages/Contact.cshtml.cs @@ -10,7 +10,7 @@ namespace TodoApi.Pages public ContactModel(ILoggerFactory logger) { - _logger = logger.CreateLogger("MyCategory"); + _logger = logger.CreateLogger("TodoApi.Pages.ContactModel.MyCategory"); } public void OnGet()