You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -313,6 +313,74 @@ Some Serilog packages require a reference to a logger configuration object. The
313
313
},
314
314
```
315
315
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:
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
+
316
384
## Arguments binding
317
385
318
386
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