Configuration, Logging, and Time Utilities
Logging Utilities
private void LogStart(string location, DateTime start)
{
_logger.LogInformation("Starting {Location} at {Start} UTC.", location, start);
}
{
_logger.LogInformation("Starting {Location} at {Start} UTC.", location, start);
}
private void LogEndMinutes(string location, DateTime start)
{
double elapsedMinutes = TimeUtils.CalcElapsedUtcMinutes(start);
if (elapsedMinutes < 1)
_logger.LogInformation("Ending {Location} after less than a minute.", location);
else
_logger.LogInformation("Ending {Location} after about {ElapsedMinutes} minutes.", location, (int)Math.Ceiling(elapsedMinutes));
}
private void LogError(string location, Exception ex)
{
_logger.LogError(ex, "[E] Error on {Location} {Error}", location, ex.Message);
}
Configuration Extensions
using Microsoft.Extensions.Configuration;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
namespace QT.KitchenMenuService.Common
{
/// <summary> Universal config extensions.</summary>
[ExcludeFromCodeCoverage]
public static class ConfigExtensions
{
public static bool GetBool(this IConfiguration configuration, string configValueName)
{
try
{
return bool.Parse(configuration.GetSection(configValueName).Value!);
}
catch
{
throw new ConfigurationErrorsException($"[E] The config value of {configValueName} is not an integer as expected.");
}
}
public static int GetInt(this IConfiguration configuration, string configValueName)
{
try
{
return int.Parse(configuration.GetSection(configValueName).Value!);
}
catch
{
throw new ConfigurationErrorsException($"[E] The config value of {configValueName} is not an integer as expected.");
}
}
/// <summary> Assumes config is stored in seconds and needs to be converted to milliseconds. </summary>
public static int GetMilliseconds(this IConfiguration configuration, string configValueName)
{
try
{
return int.Parse(configuration.GetSection(configValueName).Value!) * 1000;
}
catch
{
throw new ConfigurationErrorsException($"[E] The config value of {configValueName} is not an integer as expected.");
}
}
public static string GetString(this IConfiguration config, string key)
{
return config.GetSection(key).Value!;
}
}
}
Time Utils
namespace QT.KitchenMenuService.Common
{
public static class TimeUtils
{
public static double SecondsSinceEpoch(int minutes)
{
var now = DateTime.UtcNow.AddMinutes(Convert.ToInt32(minutes));
return Math.Round((now - DateTime.UnixEpoch).TotalSeconds);
}
public static double CalcElapsedUtcMinutes(DateTime startUtc)
{
return DateTime.UtcNow.Subtract(startUtc).TotalMinutes;
}
public static bool DidUtcHoursElapse(DateTime startUtc, int hours)
{
return CalcElapsedUtcHours(startUtc) > hours;
}
public static double CalcElapsedUtcHours(DateTime startUtc)
{
return DateTime.UtcNow.Subtract(startUtc).TotalHours;
}
public static bool DidUtcDaysElapse(DateTime startUtc, int hours)
{
return CalcElapsedUtcDays(startUtc) > hours;
}
public static double CalcElapsedUtcDays(DateTime startUtc)
{
return DateTime.UtcNow.Subtract(startUtc).TotalDays;
}
}
}
Comments
Post a Comment