ef core audit log interceptor?
i want to log all changes in my webapi project by creating a "auditLogs" table and storing table names, column names, prev and current values of the changed property etc.
i've used interceptors for the first time to override the savingChangesAsync method by inheriting SaveChangesInterceptor.
however no matter what i tried, i couldnt figure out 1) how to access the user id using the httpContext, or 2) how to access the dbContext to insert my logs into the database.
can someone help?
3 Replies
Get the context accessor by making a constructor that has it as a param
And your DbContext is named
db
You might need to cast it to the actual context you're using. I don't know if there's a better way to do that with generics or soim confused as to where to call that constructor, this is where i add the interceptor to my project but context accessor isnt here...?
using
db.Set<User>()
actually worked! i thought i had to somehow access my own dbRepository... thanks a lot