I would like to write a helper function which build the exception message to write to a log. The code look like:
if(IsWebApp)
{
&
Just check for some object that only exists in a web application, like HttpRuntime.AppVirtualPath
that SLaks suggested.
If it's a web application, you would still want to check if HttpContext.Current
is null. If the exception occurs in code that is not run beacuse of a request, it doesn't have any context. The Session_OnEnd event for example runs when a server session is removed, so it doesn't have the context.