Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Is it possible to filter out specific Polly logs in MvvmCross


Is it possible to filter out specific Polly logs in MvvmCross


Filtering out specific Polly logs in MvvmCross involves understanding both Polly's logging behavior and how MvvmCross handles logging. Here's a detailed approach to achieve this:

Understanding Polly Logging

Polly, a popular .NET resilience and transient-fault-handling library, emits telemetry logs for its operations, such as retries. These logs are not directly filterable within Polly itself; however, you can influence what gets logged by adjusting the logging level or filtering at the logging framework level.

Understanding MvvmCross Logging

MvvmCross, starting from version 8, uses Microsoft.Extensions.Logging for its logging needs. This allows you to inject `ILogger` or `ILoggerFactory` into your classes, including ViewModels and services. You can leverage this to control logging behavior.

Filtering Polly Logs

To filter out specific Polly logs in an MvvmCross application, you can't directly modify Polly's logging behavior. However, you can configure your logging framework to ignore certain messages. Here are steps to achieve this:

1. Configure Logging Framework: Ensure your logging framework (e.g., Serilog, NLog) is configured to handle log levels and filters. For example, with Serilog, you can configure it to ignore logs below a certain level or filter based on specific patterns.

2. Adjust Polly Telemetry Logs: Polly's telemetry logs, such as those for retry attempts, are always emitted. To suppress these, you might need to adjust the logging level for Polly-related logs to a level that doesn't clutter your main logs. However, this is more about adjusting the visibility rather than filtering specific logs.

3. Use Logging Framework Filtering: Implement filtering within your logging framework. For instance, with Serilog, you can use a filter to exclude messages based on their content or source.

Example with Serilog

If you're using Serilog, you can configure it to filter out specific messages. Here's an example of how you might configure Serilog to ignore Polly's retry logs:

csharp
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.Console()
    // Filter out Polly's execution attempt logs
    .Filter.ByExcluding(logEvent => logEvent.RenderMessage().Contains("Execution attempt"))
    .CreateLogger();

This approach filters out logs containing the phrase "Execution attempt," which is typical for Polly's retry telemetry.

Conclusion

While you can't directly filter Polly logs within Polly itself, you can use your logging framework to control what gets logged and what doesn't. By adjusting logging levels or implementing filters based on message content, you can effectively manage which Polly logs are visible in your application.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/logging-new
[2] https://stackoverflow.com/questions/66248766/polly-log-all-requests-with-url-headers-content-and-response
[3] https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Filtering-OUT-names-from-a-separate-table/m-p/1246285
[4] https://www.mvvmcross.com/logging
[5] https://putridparrot.com/blog/exception-handling-policies-with-polly/
[6] https://www.servicenow.com/community/developer-forum/data-filtration-excluded-tables/m-p/2590752
[7] https://stackoverflow.com/questions/77955588/how-can-i-suppress-execution-attempt-log-when-using-polly-v8-resiliencepipelineb
[8] https://github.com/App-vNext/Polly/issues/106
[9] https://www.stevejgordon.co.uk/passing-an-ilogger-to-polly-policies