Skip to content

Commit 0b7db17

Browse files
authored
Merge pull request #396 from satrapu/document-destructuring-configuration
Document destructuring configuration
2 parents cd22223 + 852b74a commit 0b7db17

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,74 @@ Some Serilog packages require a reference to a logger configuration object. The
313313
},
314314
```
315315

316+
### Destructuring
317+
318+
Destructuring means extracting pieces of information from an object and create properties with values; Serilog offers the `@` [structure-capturing operator](https://github.com/serilog/serilog/wiki/Structured-Data#preserving-object-structure). In case there is a need to customize the way log events are serialized (e.g., hide property values or replace them with something else), one can define several destructuring policies, like this:
319+
320+
```yaml
321+
"Destructure": [
322+
{
323+
"Name": "With",
324+
"Args": {
325+
"policy": "MyFirstNamespace.FirstDestructuringPolicy, MyFirstAssembly"
326+
}
327+
},
328+
{
329+
"Name": "With",
330+
"Args": {
331+
"policy": "policy": "MySecondNamespace.SecondDestructuringPolicy, MySecondAssembly"
332+
}
333+
},
334+
{
335+
"Name": "With",
336+
"Args": {
337+
"policy": "policy": "MyThirdNamespace.ThirdDestructuringPolicy, MyThirdAssembly"
338+
}
339+
},
340+
],
341+
```
342+
343+
This is how the first destructuring policy would look like:
344+
345+
```csharp
346+
namespace MyFirstNamespace;
347+
348+
public record MyDto(int Id, int Name);
349+
350+
public class FirstDestructuringPolicy : IDestructuringPolicy
351+
{
352+
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
353+
[NotNullWhen(true)] out LogEventPropertyValue? result)
354+
{
355+
if (value is not MyDto dto)
356+
{
357+
result = null;
358+
return false;
359+
}
360+
361+
result = new StructureValue(new List<LogEventProperty>
362+
{
363+
new LogEventProperty("Identifier", new ScalarValue(deleteTodoItemInfo.Id)),
364+
new LogEventProperty("NormalizedName", new ScalarValue(dto.Name.ToUpperInvariant()))
365+
});
366+
367+
return true;
368+
}
369+
}
370+
```
371+
372+
Assuming Serilog needs to destructure an argument of type **MyDto** when handling a log event:
373+
374+
```csharp
375+
logger.LogInformation("About to process input: {@MyDto} ...", myDto);
376+
```
377+
378+
it will apply **FirstDestructuringPolicy** which will convert **MyDto** instance to a **StructureValue** instance; a Serilog console sink would write the following entry:
379+
380+
```text
381+
About to process input: {"Identifier": 191, "NormalizedName": "SOME_UPPER_CASE_NAME"} ...
382+
```
383+
316384
## Arguments binding
317385

318386
When the configuration specifies a discrete value for a parameter (such as a string literal), the package will attempt to convert that value to the target method's declared CLR type of the parameter. Additional explicit handling is provided for parsing strings to `Uri`, `TimeSpan`, `enum`, arrays and custom collections.

0 commit comments

Comments
 (0)