Logging in DI
A policy registered in a DI container writes log records through ILogger. No calls or lambdas are required; the library understands each event and emits the corresponding record.
services.AddLogging();
services.AddResilience(name: "payments", policy: Resilience.Http);
// Nothing else to call. The policy logs under "NResilience.payments", which is the category
// an appsettings.json filter matches.A policy you build yourself is not logged until you say so. See Logging.
Filter per policy
Each policy logs under its own category: NResilience for a policy with no name, and NResilience.<name> otherwise. For a registered policy the name is the registration name; for an HttpClient it is the client name.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"NResilience": "Warning",
"NResilience.payments": "Debug"
}
}
}The category is fixed when the listener attaches. Because an HTTP client derives a policy per host, the category does not include the host; otherwise, a client talking to fifty hosts would create fifty categories. The host-scoped name is still included in the Policy field of every record for structured output queries.
Use ResilienceLogging.CategoryFor(name) to get the category in code.
Set the profile per policy
Set the profile per policy from a section, or for the whole process with AddResilienceLogging.
{
"Resilience": {
"payments": {
"Preset": "Http",
"Logging": "Verbose"
},
"reports": {
"Preset": "Http",
"Logging": "Off"
}
}
}An explicit WithLogging call in a configure callback overrides the automatic listener added by a container.
Provenance
Binding a configuration section is silently partial. Event 1020 reports the effective policy once per resolution at Debug; a reload produces a new entry, showing exactly what changed.
Event 1021 is a Trace companion that dumps the classifier's state. This allows you to determine what the policy will retry without reading the source code. It is guarded to ensure it costs nothing when Trace is disabled.
Next steps
- Logging: The profiles, the levels, and how to instrument a policy you built yourself.
- Event IDs: The full table, which is the contract an alert is built on.
- Options reference:
Loggingand the rest of the bindable shape.
