views:

50

answers:

2

I've found myself having a requirement to configure log4net based on a file relative to the physical location of the running ASP.NET web application. We like to start the logger as early as possible, so Application_Start seems a proper place. In IIS6, this works fine and has been running for ages, but now we moved to IIS7 and this won't work anymore:

string absolutePath = HttpContext.Current.Request.PhysicalApplicationPath;

because the HttpContext.Current is not available in many global.asax (Application, Session) events. This is old news, we all know it raises the now infamous Request is not available in this context error. We don't want to move back to Classic Mode.

Now, the question is simple: without using HttpContext, is it possible to find the physical location of the currently running web application instance?

+1  A: 

Try HttpRuntime.AppDomainAppPath. For more info, read IIS7 Integrated mode: Request is not available in this context exception in Application_Start posted by Mike Volodarsky.

Alek Davis
That's what Mikael Svenson wrote. Apparently, I didn't read the my own link well, yours and mine are the same, lol. Just tested, works like a charm :)
Abel
A: 

As an alternative answer to my own question, I'd like to add that Server.MapPath() works during the Application_Start event as well. The application domain path would then be equal to Server.MapPath("~"), but it's handier for mapping relative paths, removing the burden of concatenation (what MapPath was meant for to begin with).

Abel