Reputation: 1115
I'm developing a rest based API. For each request to the API I want to log a lot of information about that particular request- like who did the request, what resource did he ask for, keep track of how many times this client has asked for this resource, what information is he interested in and much more.
How can I do this without affecting the response time for each request? My idea is to write all this log info to Cache and set the cache expire time to 5 minutes. All incoming request within these 5 minutes will then be written to the same cache object. When the cache expires (It is possible to get a callback when it happens) I will write it to database.
Do you see any drawbacks of this method? Do you have a better suggestion?
PS! Using the built in IIS logging capabilities is not an option as it is not specific enough.
Update: What I'm actually asking here is if there is any well known problems with doing database accesses in the CacheItemRemoved callback? Is it a bad idea?
Upvotes: 1
Views: 836
Reputation: 18434
Asynchronously writing is definitely the way to go. Your proposed solution is complicated and not particularly efficient. If you don't care about writing to your log in order, the simplest thing to do would be to fire off a logging task like:
Task.Run(() => logger.Log("stuff"));
If order matters, you might also consider using an ActionBlock. See Stephen Cleary's example in a related SO question.
Upvotes: 1