6

I have an interface, let's call it ILocateLogFile, with a standard implementation for dev/beta/production servers, and one that only works in the local development environment. I can't seem to think of a nice clean way to decide (preferably at compile time, but runtime would be fine) if I'm running locally or on a server. This is a WCF application hosted on IIS, if that matters.

The best I've come up with is to use a compiler symbol, something like:

    ILocateLogFile locateLogFile;
#if DEBUG
    locateLogFile = new DevSandboxLogFileLocator();
#else
    locateLogFile = new LogFileLocator();
#endif

The problem is, compile symbols get set by the build, which I don't control, and I want to be certain. Isn't there some automagical way to check for the presence of Visual Studio? Or at least to check for Cassini rather than IIS?

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • I've seen this solved before by checking for the server name variable in an if else statement. – RandomUs1r May 28 '13 at 17:55
  • 1
    Any reason not to use .config files to configure logging? (likely you already have different .config for each environment, so may be trivial to add logging too). – Alexei Levenkov May 28 '13 at 18:12
  • @RandomUs1r that's a good suggestion, but I can't use `System.Web.HttpContext` because my WCF service isn't using ASP.Net compatibility... – McGarnagle May 28 '13 at 18:16
  • @AlexeiLevenkov I do have a reason, though maybe not a cogent one -- we're running locally from the same .config as on the Dev server. – McGarnagle May 28 '13 at 18:18

3 Answers3

9

Two ways I have done this 1 you can check the process name

bool isRunningInIisExpress = Process.GetCurrentProcess()
                                .ProcessName.ToLower().Contains("iisexpress");

Or update your config file with a custom setting

<appSettings>
    <add key="ApplicationEnvironment" value="LOCAL_DEV" />
</appSettings>

That you update specifically for each environment and have you application query for

I'm not sure if there is a way to determine this at compile time, besides having a special build configuration that is for each environment and putting a custom PRAGMA for each of these builds. Personally I think that is not as elegant, but it could also work.

jstromwick
  • 1,206
  • 13
  • 22
  • Thanks, I think the first option is exactly what I'm looking for -- although, my process name seems to be "WebDev.WebServer40" rather than "iisexpress". – McGarnagle May 28 '13 at 18:15
  • That makes sense if you're using the build in development server. You may want to think about switching over to IIS Express. I have found it's way more robust and is build into VS 2012/2010 SP1. – jstromwick May 28 '13 at 19:03
0

I found this here and it worked for me- Determine if ASP.NET application is running locally

bool isLocal = HttpContext.Current.Request.IsLocal;
Pallavi
  • 544
  • 6
  • 15
-2

Here is the code I used

If Debugger.IsAttached Then
 ' Since there is a debugger attached,
 ' assume we are running from the IDE
Else
 ' Assume we aren't running from the IDE
End If
Matze
  • 5,100
  • 6
  • 46
  • 69
Dave
  • 1