From 97404a2ebd34ec9efac1b0fbd0d2824edd2072f1 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 1 Mar 2024 15:14:49 -0800 Subject: [PATCH] Clarify the recommendation regarding log category name (#5405) --- docs/logs/README.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/logs/README.md b/docs/logs/README.md index 8172dd335..77a46c847 100644 --- a/docs/logs/README.md +++ b/docs/logs/README.md @@ -107,6 +107,21 @@ Here is the rule of thumb: Minutes - Console Application](./getting-started-console/README.md) tutorial to learn more. +:heavy_check_mark: You should use dot-separated +[UpperCamelCase](https://en.wikipedia.org/wiki/Camel_case) as the log category +name, which makes it convenient to [filter logs](#log-filtering). A common +practice is to use fully qualified class name, and if further categorization is +desired, append a subcategory name. Refer to the [.NET official +document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category) +to learn more. + +```csharp +loggerFactory.CreateLogger(); // this is equivalent to CreateLogger("MyProduct.MyLibrary.MyClass") +loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass"); // use the fully qualified class name +loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass.DatabaseOperations"); // append a subcategory name +loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass.FileOperations"); // append another subcategory name +``` + :stop_sign: You should avoid creating loggers too frequently. Although loggers are not super expensive, they still come with CPU and memory cost, and are meant to be reused throughout the application. Refer to the [logging performance @@ -186,11 +201,6 @@ instances if they are created by you. API invocation associated with the logger factory could become no-op (i.e. no logs will be emitted). -:heavy_check_mark: You should use the fully qualified class name as the log -category name. Refer to the [.NET official -document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category) -to learn more. - ## Log Correlation In OpenTelemetry, logs are automatically correlated to