I've been doing a bit of research regarding action filters and wondered if there was a way to add them programmatically to controllers??
To give some context, I'd like to add a logging filter if logging is configured in the web.config, otherwise I don't want the filter to exist in the execution chain of each action method.
I appreciate I can put a check in the actual filter code itself to see if logging is enabled but don't want to have to do this is possible.
Many thanks!
A better solution would be to use the null object pattern.
Your filter would log normally (its job is to log, not to decide how to log or what to log to, necessarily), but by default the actual logger would be an implementation that does nothing. If it's configured, the logger instance will be an implementation that logs as configured.
A simple factory could make the decision of which logger implementation to deliver to the filter or any IOC container could be configured to handle it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With