Skip to content

Commit 12583b9

Browse files
authored
Improve log template explanation (#20152)
1 parent e88c6b9 commit 12583b9

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

aspnetcore/fundamentals/logging/index.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,20 @@ Each log API uses a message template. The message template can contain placehold
317317

318318
[!code-csharp[](index/samples/3.x/TodoApiDTO/Controllers/TodoItemsController.cs?name=snippet_CallLogMethods&highlight=4,10)]
319319

320-
The order of placeholders, not their names, determines which parameters are used to provide their values. In the following code, the parameter names are out of sequence in the message template:
320+
The *order of the parameters*, not their placeholder names, determines which parameters are used to provide placeholder values in log messages. In the following code, the parameter names are out of sequence in the placeholders of the message template:
321321

322322
```csharp
323-
string p1 = "param1";
324-
string p2 = "param2";
325-
_logger.LogInformation("Parameter values: {p2}, {p1}", p1, p2);
323+
string apples = 1;
324+
string pears = 2;
325+
string bananas = 3;
326+
327+
_logger.LogInformation("Parameters: {pears}, {bananas}, {apples}", apples, pears, bananas);
326328
```
327329

328-
The preceding code creates a log message with the parameter values in sequence:
330+
However, the parameters are assigned to the placeholders in the order: `apples`, `pears`, `bananas`. The log message reflects the *order of the parameters*:
329331

330332
```text
331-
Parameter values: param1, param2
333+
Parameters: 1, 2, 3
332334
```
333335

334336
This approach allows logging providers to implement [semantic or structured logging](https://github.com/NLog/NLog/wiki/How-to-use-structured-logging). The arguments themselves are passed to the logging system, not just the formatted message template. This enables logging providers to store the parameter values as fields. For example, consider the following logger method:
@@ -1650,4 +1652,4 @@ For more information, see each provider's documentation. Third-party logging pro
16501652

16511653
* <xref:fundamentals/logging/loggermessage>
16521654

1653-
::: moniker-end
1655+
::: moniker-end

0 commit comments

Comments
 (0)