Skip to content

Commit bc21976

Browse files
committed
Document destructuring configuration
1 parent cd22223 commit bc21976

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,80 @@ 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 [@ destructuring 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 class MyDto
349+
{
350+
public int Id {get; set;}
351+
public int Name {get; set;}
352+
}
353+
354+
public class FirstDestructuringPolicy : IDestructuringPolicy
355+
{
356+
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
357+
{
358+
result = null;
359+
MyDto dto = value as MyDto;
360+
361+
if (dto == null)
362+
{
363+
return false;
364+
}
365+
366+
result = new StructureValue(new List<LogEventProperty>
367+
{
368+
new LogEventProperty("Identifier", new ScalarValue(deleteTodoItemInfo.Id)),
369+
new LogEventProperty("NormalizedName", new ScalarValue(dto.Name.ToUpperInvariant()))
370+
});
371+
372+
return true;
373+
}
374+
}
375+
}
376+
```
377+
378+
Assuming Serilog needs to destructure an argument of type **MyDto** when handling a log event:
379+
380+
```csharp
381+
logger.LogInformation("About to process input: {@MyDto} ...", myDto);
382+
```
383+
384+
it will apply **FirstDestructuringPolicy** which will convert **MyDto** instance to a **StructureValue** instance; a Serilog console sink would write the following entry:
385+
386+
```text
387+
About to process input: {"Identifier": 191, "NormalizedName": "SOME_UPPER_CASE_NAME"} ...
388+
```
389+
316390
## Arguments binding
317391

318392
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)