I created a custom action filter to perform logging for auditing trails.I added my logging code to public override void OnActionExecuting(ActionExecutingContext filterContext).
My question is, how do I pass my EF dbContext to this method? I’d like to write a single action filter and re-use it on other development projects without changing the dbcontext for every project.
If this isn’t a recommended practice, what should I do?
If I were doing such a thing, I think I would be implementing a generic audit logging method in the service layer that already is aware of the data context and pass on an audit model to it. This way, if there’s ever a need to log different parts of the application (that may not even be related to controllers), you don’t have to re-implement anything.
Alternatively if you want to stick to just controllers you could perhaps make an interface for the data context
Create a BaseController that implements in along with
System.Web.Mvc.ControllerYou can use this base class on controllers and reach the context via
DataContext, but for logging, you can create a new class with your overridden methodAnd then inherit your stuff from it
Although this isn’t the most straight forward solution, you could lose the interface all together and do everything in the
BaseController, but I’m just throwing stuff on the board in case you see something that would work for you.