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
+74Lines changed: 74 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -313,6 +313,80 @@ 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 [@ 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:
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
+
316
390
## Arguments binding
317
391
318
392
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