Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

A coworker of mine has created a centralized Logging utility that works throughout our SP projects. It does a good job of queuing up a large number of logs, deals with logging level, etc.

However, it utilizes HttpContext.Cache in order to do some caching. This causes it to break when I attempt to Log from a custom timer job that I've written.

Can anyone think of a good way to fix this so that it'll work in a Timer, without completely tearing apart the architecture and re-writing it? Are there any System.Web.Caching.Cache objects that exist, or can be created, from within the SharePoint timer?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

HttpContext.Cache uses HttpRuntime.Cache as the real underlying implementation, and that is not bound to a web context (despite the name implying otherwise). It can be used in any application (class library, WinForms, etc.) without issue - other than it can look a little jarring to pull in System.Web.

Your colleague's utility should be using a cache abstraction (if not, a good opportunity to replace it) so you can use dependency injection to define the specific implementation.

Also important to consider especially in the context of a SharePoint Timer Job - the cache is server-local, so if you're running a multi-server farm and you're not carefully controlling which server these jobs are executed on, it makes the cache essentially useless at best (or worst, introduces some odd bugs). If you need a cache that's distributed across the farm, consider something like AppFabric instead of .NET's native web cache.

share|improve this answer
    
Weird that when I did a search for your suggestion, people argued that HttpRuntime.Cache won't work in a timer. But when I tried to do it, it seemed to work. I'll have to do a bit more testing before I mark your answer as correct though. Thanks! –  Craig Sep 21 '12 at 12:10
    
Ok, I had a chance to get back to this. The cache appears to be working fine, but there are other problems since the Logger reads some web.config information to figure out which SharePoint list to log to. Again, web.config is not available from the configuration Manager in the context of a timer. So now I have to figure this out. Anyway, thanks for your advice with HttpRuntime.Cache. –  Craig Sep 21 '12 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.