diff --git a/.editorconfig b/.editorconfig
index 1b25fe4..9f10270 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -5,6 +5,7 @@ root = true
# All files
[*]
indent_style = space
+guidelines = 100, 160
# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 936bf72..780af47 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
-Nothing yet.
+### Added
+
+- Implemented `EnvironmentHelper` and tests.
## [1.21.1] - 2025-11-29
diff --git a/src/Logitar/EnvironmentHelper.cs b/src/Logitar/EnvironmentHelper.cs
new file mode 100644
index 0000000..c9bb531
--- /dev/null
+++ b/src/Logitar/EnvironmentHelper.cs
@@ -0,0 +1,96 @@
+namespace Logitar;
+
+///
+/// Defines helper methods for environment variables.
+///
+public static class EnvironmentHelper
+{
+ ///
+ /// Retrieves the boolean value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static bool GetBoolean(string variable, bool defaultValue = false) => TryGetBoolean(variable) ?? defaultValue;
+ ///
+ /// Retrieves the boolean value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static bool? TryGetBoolean(string variable)
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && bool.TryParse(value.Trim(), out bool boolean) ? boolean : null;
+ }
+
+ ///
+ /// Retrieves the enum value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static T GetEnum(string variable, T defaultValue = default) where T : struct, Enum
+ {
+ return TryGetEnum(variable) ?? defaultValue;
+ }
+ ///
+ /// Retrieves the enum value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static T? TryGetEnum(string variable) where T : struct, Enum
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && Enum.TryParse(value, out T enumValue) && Enum.IsDefined(typeof(T), enumValue) ? enumValue : null;
+ }
+
+ ///
+ /// Retrieves the integer value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static int GetInt32(string variable, int defaultValue = 0) => TryGetInt32(variable) ?? defaultValue;
+ ///
+ /// Retrieves the integer value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static int? TryGetInt32(string variable)
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int integer) ? integer : null;
+ }
+
+ ///
+ /// Retrieves the string value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static string GetString(string variable, string defaultValue = "") => TryGetString(variable) ?? defaultValue;
+ ///
+ /// Retrieves the string value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static string? TryGetString(string variable) => Environment.GetEnvironmentVariable(variable)?.CleanTrim();
+
+ ///
+ /// Retrieves the TimeSpan value of an environment variable.
+ ///
+ /// The name of the variable.
+ /// The default value.
+ /// The value of the environment value.
+ public static TimeSpan GetTimeSpan(string variable, TimeSpan defaultValue = default) => TryGetTimeSpan(variable) ?? defaultValue;
+ ///
+ /// Retrieves the TimeSpan value of an environment variable. Returns null if it is not found.
+ ///
+ /// The name of the variable.
+ /// The value of the environment value, or null if not found.
+ public static TimeSpan? TryGetTimeSpan(string variable)
+ {
+ string value = Environment.GetEnvironmentVariable(variable);
+ return !string.IsNullOrWhiteSpace(value) && TimeSpan.TryParse(value.Trim(), out TimeSpan timeSpan) ? timeSpan : null;
+ }
+}
diff --git a/tests/Logitar.UnitTests/EnvironmentHelperTests.cs b/tests/Logitar.UnitTests/EnvironmentHelperTests.cs
new file mode 100644
index 0000000..b960235
--- /dev/null
+++ b/tests/Logitar.UnitTests/EnvironmentHelperTests.cs
@@ -0,0 +1,187 @@
+namespace Logitar;
+
+[Trait(Traits.Category, Categories.Unit)]
+public class EnvironmentHelperTests
+{
+ private const string VariableName = "MyVariable";
+
+ [Theory(DisplayName = "GetBoolean: it should return the found variable value.")]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void Given_Found_When_GetBoolean_Then_BooleanValue(bool value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ bool variable = EnvironmentHelper.GetBoolean(VariableName);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Theory(DisplayName = "GetBoolean: it should return the default variable value.")]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void Given_NotFound_When_GetBoolean_Then_DefaultValue(bool defaultValue)
+ {
+ bool variable = EnvironmentHelper.GetBoolean(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
+ [Theory(DisplayName = "GetEnum: it should return the found variable value.")]
+ [InlineData(DayOfWeek.Sunday)]
+ public void Given_Found_When_GetEnum_Then_BooleanValue(DayOfWeek value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ DayOfWeek variable = EnvironmentHelper.GetEnum(VariableName);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Theory(DisplayName = "GetEnum: it should return the default variable value.")]
+ [InlineData(DayOfWeek.Friday)]
+ public void Given_NotFound_When_GetEnum_Then_DefaultValue(DayOfWeek defaultValue)
+ {
+ DayOfWeek variable = EnvironmentHelper.GetEnum(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
+ [Theory(DisplayName = "GetInt32: it should return the found variable value.")]
+ [InlineData(42)]
+ public void Given_Found_When_GetInt32_Then_BooleanValue(int value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ int variable = EnvironmentHelper.GetInt32(VariableName);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Theory(DisplayName = "GetInt32: it should return the default variable value.")]
+ [InlineData(-1)]
+ public void Given_NotFound_When_GetInt32_Then_DefaultValue(int defaultValue)
+ {
+ int variable = EnvironmentHelper.GetInt32(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
+ [Theory(DisplayName = "GetString: it should return the found variable value.")]
+ [InlineData(" Test ")]
+ public void Given_Found_When_GetString_Then_BooleanValue(string value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value);
+ string variable = EnvironmentHelper.GetString(VariableName);
+ Assert.Equal(value.Trim(), variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Theory(DisplayName = "GetString: it should return the default variable value.")]
+ [InlineData("<>")]
+ public void Given_NotFound_When_GetString_Then_DefaultValue(string defaultValue)
+ {
+ string variable = EnvironmentHelper.GetString(VariableName, defaultValue);
+ Assert.Equal(defaultValue, variable);
+ }
+
+ [Theory(DisplayName = "GetTimeSpan: it should return the found variable value.")]
+ [InlineData(" 12:34:56 ")]
+ public void Given_Found_When_GetTimeSpan_Then_BooleanValue(string value)
+ {
+ TimeSpan timeSpan = TimeSpan.Parse(value);
+ Environment.SetEnvironmentVariable(VariableName, value);
+ TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName);
+ Assert.Equal(timeSpan, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Theory(DisplayName = "GetTimeSpan: it should return the default variable value.")]
+ [InlineData("00:00:00")]
+ public void Given_NotFound_When_GetTimeSpan_Then_DefaultValue(string defaultValue)
+ {
+ TimeSpan timeSpan = TimeSpan.Parse(defaultValue);
+ TimeSpan variable = EnvironmentHelper.GetTimeSpan(VariableName, timeSpan);
+ Assert.Equal(timeSpan, variable);
+ }
+
+ [Theory(DisplayName = "TryGetBoolean: it should return the found variable value.")]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void Given_Found_When_TryGetBoolean_Then_BooleanValue(bool value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ bool? variable = EnvironmentHelper.TryGetBoolean(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetBoolean: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetBoolean_Then_BooleanValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetBoolean(VariableName));
+ }
+
+ [Theory(DisplayName = "TryGetEnum: it should return the found variable value.")]
+ [InlineData(DayOfWeek.Saturday)]
+ public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ DayOfWeek? variable = EnvironmentHelper.TryGetEnum(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetEnum: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetEnum_Then_BooleanValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetEnum(VariableName));
+ }
+
+ [Theory(DisplayName = "TryGetInt32: it should return the found variable value.")]
+ [InlineData(999)]
+ public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ int? variable = EnvironmentHelper.TryGetInt32(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value, variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetInt32: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetInt32_Then_BooleanValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetInt32(VariableName));
+ }
+
+ [Theory(DisplayName = "TryGetString: it should return the found variable value.")]
+ [InlineData(" Test ")]
+ public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value.ToString());
+ string? variable = EnvironmentHelper.TryGetString(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(value.Trim(), variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetString: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetString_Then_BooleanValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetString(VariableName));
+ }
+
+ [Theory(DisplayName = "TryGetTimeSpan: it should return the found variable value.")]
+ [InlineData(" 7.00:00:00 ")]
+ public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
+ {
+ Environment.SetEnvironmentVariable(VariableName, value);
+ TimeSpan? variable = EnvironmentHelper.TryGetTimeSpan(VariableName);
+ Assert.NotNull(variable);
+ Assert.Equal(TimeSpan.Parse(value.Trim()), variable);
+ Environment.SetEnvironmentVariable(VariableName, null);
+ }
+
+ [Fact(DisplayName = "TryGetTimeSpan: it should return null when the variable is not found.")]
+ public void Given_NotFound_When_TryGetTimeSpan_Then_BooleanValue()
+ {
+ Assert.Null(EnvironmentHelper.TryGetTimeSpan(VariableName));
+ }
+}