Stack Exchange
ASP.NET Core ignores ASPNET_ENV and Hosting:Environment
No matter when or where I set either ASPNET_ENV or Hosting:Environment the startup code will always enter 
//This method is invoked when ASPNET_ENV is 'Production'
//The allowed values are Development,Staging and Production
public void ConfigureProduction(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
   loggerFactory.AddConsole(minLevel: LogLevel.Warning);

   Configure(app);
}


What I've tried so far

Set Hosting:Environment to Development in the project properties
Set ASPNET_ENV to Development in the project properties
Set Hosting:Environment to Development in launchSettings.json
Set ASPNET_ENV to Development in launchSettings.json
Set ASPNET_ENV to Development in code via Environment.SetEnvironmentVariable("ASPNET_ENV", "Development"); in the Startup method before the call to ConfigurationBuilder.GetEnvironmentVariables()

This is version 1.0.0-rc2-20143 by the way. Am I missing something here or is it just a bug?

The environment variable name has been changed to ASPNETCORE_ENVIRONMENT as part of the name change.
It was announced in this issue and change in this PR.

Thanks, that was indeed the problem.

I have to actually set ASPNET_ENVIRONMENT as well, not sure what is going on here.

Turns out that editing the path variables changes launchSettings.json but not the other way round. Make sure you set ASPNETCORE_ENVIRONMENT in the project properties only.

If you have not done it already, try adding the environment variables to the configuration instance.
public static void Main(string[] args)
{
    var builder = new ConfigurationBuilder();
    builder.AddJsonFile("appsettings.json");
    builder.AddEnvironmentVariables();
    var config = builder.Build();
}


Done that, makes no difference. Kestrel output in console window keeps claiming Hosting environment: Production. I'm inclined to say this is indeed a bug.

Whilst the answer from Henk Mollema is correct for the environment variable name, I still encountered issues where dotnetcore would seem to ignore the environment variable and use appsettings.json when run from command line.
To ensure it uses the correct environment and app settings, try the following:

Change the environment variable via Project Properties -> Debug -> Environment Variables -> ASPNETCORE_ENVIRONMENT -> Value : Change the value to match your intended environment eg Staging
Build Solution
From commandline, type SETX ASPNETCORE_ENVIRONMENT "YOUR-ENVIRONMENT-NAME" where replace "YOUR-ENVIRONMENT-NAME" to match what you set in step 1 eg Staging and ensure you include the "quotes"
IMPORTANT - make sure you all your various appsettings.json eg appsettings.staging.json is present in the project directory where you will run it. (In the case of a published solution, the appsettings.staging.json may not have been copied, so ensure it's there)
From Commandline, go to your project directory (or published directory) and run your project by typing dotnet run (or "dotnet YOURPROJECTNAME.DLL" for published projects) 
Observe in the next line that appears in the commandline which states Hosting environment: YOURENVIRONMENTNAME eg Hosting environment:staging


Running a DotNetCore project from Visual Studio always picked the
  correct appsettings based on the environment set in your project
  properties, however these are the steps I followed to run dotnet core correctly from commandline
  working correctly.


You mean launchSettings.json, not appsettings.json, right? Normally appsettings.json isn't even loaded until after the Environment determined.  In any case, if you look at the Asp.Net Core 2.0 source code, there is only ONE way to change the built-in IHostingEnvironment's Environment property. That's via the ASPNETCORE_ENVIRONMENT environment variable. It's because the Microsoft.AspNetCore.Hosting.Internal.HostingEnvironment object is setup during WebHostBuilder.Build(), but before your ConfigureAppConfig() or ConfigureServices() delegate/lambda runs.

(facepalm) My bad; I totally spaced that there's actually 2: You can also use the WebHostBuilder.UseEnvironment() extension method to change it before you call .Build().

This worked for me using .net core 2.0 and building a web api.  Notice the 2 different methods of accessing the environment variable.
Program.cs
public static void Main(string[] args)
    {

        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args){

        if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ") == "Production"){
            return WebHost.CreateDefaultBuilder(args)
             .UseKestrel()
             .UseContentRoot(Directory.GetCurrentDirectory())
             .UseIISIntegration()
             .UseStartup<Startup>()
             .Build();
        } else {
            return WebHost.CreateDefaultBuilder(args)
             .UseStartup<Startup>()
             .Build();
        }
    }

But in the startup file there is another environment option
Startup.cs
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                .AllowAnyMethod()
                                                                 .AllowAnyHeader()));
        var connectionString = "Data Source=tcp:<some-ip>,<some-port>;Initial Catalog=<some database>;Integrated Security=False;User Id=SA;Password=<some password>;MultipleActiveResultSets=True";
        services.AddDbContext<myContext>(opt => opt.UseSqlServer(connectionString));
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseCors("AllowAll");
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

