skip to content

A ferry-booking site on ASP.NET Core 10 logs 'Hosting environment: Production' on its staging server although ops set ASPNETCORE_ENVIRONMENT=Staging; how do you find out why?

level: seniorimportance: should knowfreq 30%

answer

  1. the variable must reach the process
  2. service manager, not your SSH shell
  3. DOTNET_ENVIRONMENT or --environment outranks it
  4. EnvironmentName rewrites web.config at publish
  5. read the running process's own environment

basics

~20 s

Check what the running process actually received. The variable may sit in the wrong scope (an SSH shell instead of the service or container), be outranked by DOTNET_ENVIRONMENT, --environment or WebApplicationOptions, or be lost when web.config is republished.

solid answer

~40 s

Start from the log: `Hosting environment: Production` means the `environment` key of host configuration was empty or said Production, because Production is the fallback. First prove whether `ASPNETCORE_ENVIRONMENT` reached the process at all: on Linux read the process's environment rather than your shell's, since `export` in an SSH session never reaches a service. Then look for anything that outranks it under `WebApplication`: a `DOTNET_ENVIRONMENT` set machine-wide or by the base image, an `--environment` argument in the start command, or a hard-coded `WebApplicationOptions.EnvironmentName`. On IIS, check that publishing did not rewrite `web.config`, since the `EnvironmentName` MSBuild property writes its own `ASPNETCORE_ENVIRONMENT` entry there. Finally make the fix stick: set the value in exactly one place per host, restart the process, and read `IWebHostEnvironment.EnvironmentName`, never the raw variable, when you need it in code.

code

bash · 6 lines
bash
# What the running app actually received (Linux)
pid=$(pgrep -f FerryBooking.dll)
tr '\0' '\n' < /proc/$pid/environ | grep -E 'ENVIRONMENT'

# What the service will pass on its next start (systemd)
systemctl show ferry-booking.service -p Environment

go deeper

for a junior

Recall that Production in the startup log means nothing set a usable environment name for this process.

for a middle

List the sources that can set or outrank ASPNETCORE_ENVIRONMENT and how to read a running process's environment.

for a senior

Work the diagnosis in order: delivery to the process, precedence, then deployment rewrites, and make the fix verifiable.

for a principal

Standardise how environments are declared and asserted across hosts so this failure becomes a failed deployment, not a quiet misconfiguration.

## Start from what the log tells you The `Microsoft.Hosting.Lifetime` logger writes `Hosting environment: {EnvName}` right after `Application started`. That value is `IWebHostEnvironment.EnvironmentName`, the name the framework actually used. Production there means one of two things: the `environment` key of host configuration was **empty**, so the host fell back to `Environments.Production`, or some source **explicitly said Production**. Ops' belief that they set `ASPNETCORE_ENVIRONMENT=Staging` is a claim about some environment, not necessarily about this process. ## Step 1: did the variable reach the process? Environment variables are per process and inherited only from the parent that starts it. - **Linux service**: `export ASPNETCORE_ENVIRONMENT=Staging` in an SSH session changes that shell and its children only. A service gets its environment from its unit definition (for systemd, an `Environment=` or `EnvironmentFile=` line), and needs a daemon reload and restart after a change. Read `/proc/<pid>/environ` of the running app to see what it really has. - **Container**: an `ENV` in the Dockerfile is a default that a runtime `-e` or compose `environment:` entry overrides, so check what the orchestrator passes, not only what the image declares. - **IIS**: the ASP.NET Core Module passes variables from the `<environmentVariables>` element of the site's `web.config`, or from an application-pool environment variable; the worker process must restart to see a change. - **Any host**: a process started before the variable was set keeps its old environment until it restarts. ## Step 2: is something outranking it? Under `WebApplication.CreateBuilder` in ASP.NET Core 10 the sources are added in this order, and the last one with a value wins: 1. `ASPNETCORE_ENVIRONMENT` 2. `DOTNET_ENVIRONMENT` 3. an `--environment` command-line argument 4. `WebApplicationOptions.EnvironmentName` passed to `CreateBuilder` So `DOTNET_ENVIRONMENT=Production`, set machine-wide for some worker service or baked into a base image, silently defeats a correct `ASPNETCORE_ENVIRONMENT=Staging`. Check the service's start command for `--environment Production`, and search `Program.cs` for a hard-coded `EnvironmentName`. You can rule out a late `builder.WebHost.UseEnvironment(...)` call: in ASP.NET Core 10 that throws `NotSupportedException` rather than silently switching. ## Step 3: is the deployment overwriting it? - **`web.config` rewritten on publish.** When the `EnvironmentName` MSBuild property is set in the project or a publish profile, `dotnet publish` writes an `ASPNETCORE_ENVIRONMENT` entry with that value into the published `web.config`, replacing one that was edited by hand on the server at the next deployment. - **launchSettings.json assumed to travel.** It never does: the Web SDK marks it `CopyToPublishDirectory="Never"`, and `dotnet FerryBooking.dll` would not read it anyway. - **A typo.** `Stagign` is a legal custom name, but it would log `Stagign`, not Production; if the log says Production, look for a missing or overridden value instead. ## A worked example On the ferry-booking staging host the sequence usually looks like this: 1. The startup log says Production, so either the key was empty or something said Production. 2. Reading the process's environment shows `ASPNETCORE_ENVIRONMENT=Staging` **and** `DOTNET_ENVIRONMENT=Production`, the latter inherited from a host-wide setting added for a nightly worker service. 3. Because the `DOTNET_` source is added after the `ASPNETCORE_` source, the worker's variable wins for the web app as well. 4. The fix is to scope the worker's variable to the worker's own service definition, or to move both apps to `DOTNET_ENVIRONMENT` with per-service values, then restart and re-read the startup log. Had the process environment shown neither variable, the problem would have been delivery, not precedence, and the fix would belong in the service definition. ## Making the fix stick | Host | Set it in | Verify with | |---|---|---| | Linux service | the service unit's environment | the process's own environment and the startup log | | Container | the deployment manifest's environment section | the container's environment and the startup log | | IIS | `web.config` via the `EnvironmentName` property, or the app pool | the startup log after a recycle | - Set the environment in **one** place per host and remove competing variables. - Prefer one variable name across the estate so web apps and workers cannot disagree. - In code, read `IWebHostEnvironment.EnvironmentName`, never `Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")`, because the raw variable can differ from the value the host resolved. - Consider failing fast at startup when the name is not one of the environments the team deploys, so a missing value is caught by a crash instead of by a customer.

  • Why not check Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") in code to decide staging behaviour?
    Because the host does not rely on that variable alone: DOTNET_ENVIRONMENT, --environment and WebApplicationOptions can override it, so the raw variable can disagree with the environment the framework used to pick configuration files and defaults. Read IWebHostEnvironment.EnvironmentName or use IsEnvironment instead.
  • How would you stop a silent Production start from recurring?
    Make the environment explicit and asserted: set it in one place per host, alert on the startup log line, and have Program.cs throw when the resolved name is not in the list of environments the team deploys. A missing value then fails the deployment instead of running staging traffic with production defaults.

  • Running export ASPNETCORE_ENVIRONMENT=Staging in an SSH session changes the running service.
  • Once ASPNETCORE_ENVIRONMENT is set, no other source can override it.
  • Copying launchSettings.json to the server fixes the environment.
  • Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") always equals IWebHostEnvironment.EnvironmentName.