Utils.EnvironmentManager 3.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Utils.EnvironmentManager --version 3.0.4                
NuGet\Install-Package Utils.EnvironmentManager -Version 3.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Utils.EnvironmentManager" Version="3.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Utils.EnvironmentManager --version 3.0.4                
#r "nuget: Utils.EnvironmentManager, 3.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Utils.EnvironmentManager as a Cake Addin
#addin nuget:?package=Utils.EnvironmentManager&version=3.0.4

// Install Utils.EnvironmentManager as a Cake Tool
#tool nuget:?package=Utils.EnvironmentManager&version=3.0.4                

Project logo

Utils.EnvironmentManager

SonarCloud

Quality Gate Status CodeFactor

NuGet version License


The EnvironmentManager namespace now provides a class EnvManager that uses AutoMapper package, for retrieving environment variable values and performing type conversions.

Note This documentation assumes a basic understanding of AutoMapper library. AutoMapper docs

Initialization

EnvManager initialization can be achieved with or without a custom AutoMapper configuration:

  1. Without a custom configuration:
var manager = new EnvManager();
  1. With a custom configuration:
var manager = new EnvManager(config: config);

Methods

GetEnvironmentValue

The method retrieves the value of the specified environment variable and converts it to the desired type using AutoMapper.

Signature:

public object GetEnvironmentValue(Type type, string variableName, bool raiseException = false)

Parameters:

  • type (Type): The type to which the environment variable's value should be converted.
  • variableName (string): The name of the environment variable.
  • raiseException (bool, optional): Specifies whether to raise an exception if the environment variable is null or empty, or when the conversion fails. Defaults to false.

Returns:

  • object: The converted value of the environment variable.

GetEnvironmentValue<T>

This method retrieves the value of the specified environment variable and converts it to the specified type T.

Signature:

public T GetEnvironmentValue<T>(string variableName, bool raiseException = false)

Parameters:

  • variableName (string): The name of the environment variable.
  • raiseException (bool, optional): Specifies whether to raise an exception if the environment variable is null or empty, or when the conversion fails. Defaults to false.

Returns:

  • T: The converted value of the environment variable.

Adding Custom Mappings

The library now uses AutoMapper for type conversions. Therefore, to add custom type conversions, you can utilize the EnvManagerMappingConfigurator class.

Example:

var config = new EnvManagerMappingConfigurator()
    .CreateMapFor(x => DateTime.ParseExact(x, "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture))
    .CreateMapFor(x => Enum.Parse<MyEnumeration>(x, true))
    .Build();

var manager = new EnvManager(config: config);

DateTime customDateFormat = manager.GetEnvironmentValue<DateTime>("CUSTOM_DATE_FORMAT");

In this example, a custom date format is added using the CreateMapFor method. Also in this example adding mapping for a MyEnumeration enum. Once the custom mappings are added, the configuration is built and passed to the EnvManager.

Logging

EnvManager incorporates logging through the Microsoft's ILogger interface, providing insights into the operations and potential issues while working with environment variables.

Logger Initialization

You can pass an instance of ILogger<EnvManager> when creating the EnvManager. If no logger is provided, a default instance of NullLogger<EnvManager> is used, which means no logging output will be produced.

Example:

var logger = new LoggerFactory().CreateLogger<EnvManager>();
var manager = new EnvManager(logger: logger);

If you wish to use the default logger (which won't produce any log output):

var manager = new EnvManager();

Logging Scenarios

Here are some situations where the EnvManager logs information:

  1. Warning: If an environment variable is null or empty and the raiseException parameter is set to false, a warning log will be generated.
  • Log Message: "Environment variable '{VariableName}' is null or empty."
  1. Error: If there's a failed conversion of an environment variable and the raiseException parameter is set to false, an error log will be created.
  • Log Message: "Failed to convert environment variable '{VariableName}' to type '{Type}'. Returning default value."

In both scenarios, the actual variable name and type (if applicable) will replace the placeholders {VariableName} and {Type}.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Utils.EnvironmentManager:

Package Downloads
ConfiguredSqlConnection

The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings.

BNBParty.GraphQLClient

GraphQL Client for BNBParty .NET

EthSmartContractIO.SecretsProvider

A EthSmartContractIO module, that facilitates the secure creation of an Ethereum account and the extraction of secrets.

DownloaderV3.Source.CovalentLastBlock

The module for fetching the latest block data using Covalent API in the DownloaderV3 system.<

DownloaderV3.Source.CovalentDocument

The module for fetching, decoding, and saving blockchain event data using Covalent API in the DownloaderV3 system.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0 221 10/22/2024
4.0.1 663 8/27/2024
3.0.4 624 6/9/2024
3.0.3 88 6/9/2024
3.0.2 102 6/8/2024
3.0.1 98 6/6/2024
3.0.0 157 6/3/2024
2.0.1 3,934 8/7/2023
2.0.0 153 8/6/2023
1.1.0 2,582 6/15/2023
1.0.0 702 5/15/2023