I think the easiest solution is to read the value from the ASPNETCORE_ENVIRONMENT
environment variable and compare it with Environments.Development
:
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var isDevelopment = environment == Environments.Development;
.NET 6 or higher
Starting from .NET 6 using the new application bootstrapping model you can access the environment from the application builder:
var builder = WebApplication.CreateBuilder(args);
var isDevelopment = builder.Environment.IsDevelopment();