Skip to content

Commit 97404a2

Browse files
authored
Clarify the recommendation regarding log category name (#5405)
1 parent b754b13 commit 97404a2

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

docs/logs/README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ Here is the rule of thumb:
107107
Minutes - Console Application](./getting-started-console/README.md) tutorial
108108
to learn more.
109109

110+
:heavy_check_mark: You should use dot-separated
111+
[UpperCamelCase](https://en.wikipedia.org/wiki/Camel_case) as the log category
112+
name, which makes it convenient to [filter logs](#log-filtering). A common
113+
practice is to use fully qualified class name, and if further categorization is
114+
desired, append a subcategory name. Refer to the [.NET official
115+
document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category)
116+
to learn more.
117+
118+
```csharp
119+
loggerFactory.CreateLogger<MyClass>(); // this is equivalent to CreateLogger("MyProduct.MyLibrary.MyClass")
120+
loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass"); // use the fully qualified class name
121+
loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass.DatabaseOperations"); // append a subcategory name
122+
loggerFactory.CreateLogger("MyProduct.MyLibrary.MyClass.FileOperations"); // append another subcategory name
123+
```
124+
110125
:stop_sign: You should avoid creating loggers too frequently. Although loggers
111126
are not super expensive, they still come with CPU and memory cost, and are meant
112127
to be reused throughout the application. Refer to the [logging performance
@@ -186,11 +201,6 @@ instances if they are created by you.
186201
API invocation associated with the logger factory could become no-op (i.e. no
187202
logs will be emitted).
188203

189-
:heavy_check_mark: You should use the fully qualified class name as the log
190-
category name. Refer to the [.NET official
191-
document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category)
192-
to learn more.
193-
194204
## Log Correlation
195205

196206
In OpenTelemetry, logs are automatically correlated to

0 commit comments

Comments
 (0)