forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentDetector.cs
More file actions
62 lines (56 loc) · 2.15 KB
/
EnvironmentDetector.cs
File metadata and controls
62 lines (56 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Microsoft. All rights reserved.
namespace KernelMemory.Core.Logging;
/// <summary>
/// Detects the current runtime environment from environment variables.
/// Environment detection is critical for security decisions like sensitive data scrubbing.
/// </summary>
public static class EnvironmentDetector
{
/// <summary>
/// Gets the current environment name.
/// Checks DOTNET_ENVIRONMENT first, falls back to ASPNETCORE_ENVIRONMENT,
/// then defaults to Development if neither is set.
/// </summary>
/// <returns>The environment name (e.g., "Development", "Production", "Staging").</returns>
public static string GetEnvironment()
{
// Check DOTNET_ENVIRONMENT first (takes precedence)
var dotNetEnv = Environment.GetEnvironmentVariable(LoggingConstants.DotNetEnvironmentVariable);
if (!string.IsNullOrWhiteSpace(dotNetEnv))
{
return dotNetEnv;
}
// Fall back to ASPNETCORE_ENVIRONMENT
var aspNetEnv = Environment.GetEnvironmentVariable(LoggingConstants.AspNetCoreEnvironmentVariable);
if (!string.IsNullOrWhiteSpace(aspNetEnv))
{
return aspNetEnv;
}
// Default to Development for safety (full logging)
return LoggingConstants.DefaultEnvironment;
}
/// <summary>
/// Checks if the current environment is Production.
/// In Production, sensitive data is scrubbed from logs.
/// </summary>
/// <returns>True if running in Production environment.</returns>
public static bool IsProduction()
{
return string.Equals(
GetEnvironment(),
LoggingConstants.ProductionEnvironment,
StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Checks if the current environment is Development.
/// In Development, full logging is enabled for debugging.
/// </summary>
/// <returns>True if running in Development environment.</returns>
public static bool IsDevelopment()
{
return string.Equals(
GetEnvironment(),
LoggingConstants.DefaultEnvironment,
StringComparison.OrdinalIgnoreCase);
}
}