Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document destructuring configuration #396

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,74 @@ Some Serilog packages require a reference to a logger configuration object. The
},
```

### Destructuring

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:

```yaml
"Destructure": [
{
"Name": "With",
"Args": {
"policy": "MyFirstNamespace.FirstDestructuringPolicy, MyFirstAssembly"
}
},
{
"Name": "With",
"Args": {
"policy": "policy": "MySecondNamespace.SecondDestructuringPolicy, MySecondAssembly"
}
},
{
"Name": "With",
"Args": {
"policy": "policy": "MyThirdNamespace.ThirdDestructuringPolicy, MyThirdAssembly"
}
},
],
```

This is how the first destructuring policy would look like:

```csharp
namespace MyFirstNamespace;

public record MyDto(int Id, int Name);

public class FirstDestructuringPolicy : IDestructuringPolicy
{
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory,
[NotNullWhen(true)] out LogEventPropertyValue? result)
{
if (value is not MyDto dto)
{
result = null;
return false;
}

result = new StructureValue(new List<LogEventProperty>
{
new LogEventProperty("Identifier", new ScalarValue(deleteTodoItemInfo.Id)),
new LogEventProperty("NormalizedName", new ScalarValue(dto.Name.ToUpperInvariant()))
});

return true;
}
}
```

Assuming Serilog needs to destructure an argument of type **MyDto** when handling a log event:

```csharp
logger.LogInformation("About to process input: {@MyDto} ...", myDto);
```

it will apply **FirstDestructuringPolicy** which will convert **MyDto** instance to a **StructureValue** instance; a Serilog console sink would write the following entry:

```text
About to process input: {"Identifier": 191, "NormalizedName": "SOME_UPPER_CASE_NAME"} ...
```

## Arguments binding

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.
Expand Down