Skip to content

Commit

Permalink
Clarify the recommendation regarding log category name (#5405)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Mar 1, 2024
1 parent b754b13 commit 97404a2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions docs/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyClass>(); // 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 97404a2

Please sign in to comment.