Added Check & Added Loglevel via env

This commit is contained in:
saile2204 2023-03-20 21:07:37 +01:00
parent 63fba2b49e
commit e1f5e43c43
3 changed files with 50 additions and 23 deletions

View File

@ -8,9 +8,19 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using RestSharp;
static T ParseEnum<T>(string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
LogLevel loglevel;
if (Environment.GetEnvironmentVariables()["LogLevel"] != null)
loglevel = ParseEnum<LogLevel>(Environment.GetEnvironmentVariables()["LogLevel"].ToString());
else
loglevel = LogLevel.Information;
var configuration = builder.Build();
@ -21,13 +31,20 @@ logConfig.CurrentValue.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed
var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(configuration)
.AddLogging(logging => logging.AddSpecterConsoleLogger(configuration =>
.AddLogging(logging =>
{
logging.AddSpecterConsoleLogger(configuration =>
{
// Replace warning value from appsettings.json of "Cyan"
configuration.LogLevelToColorMap[LogLevel.Warning] = ConsoleColor.DarkCyan;
configuration.LogLevelToColorMap[LogLevel.Debug] = ConsoleColor.Yellow;
// Replace warning value from appsettings.json of "Red"
configuration.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed;
}))
});
logging.SetMinimumLevel(loglevel);
})
.AddSingleton<ITimerService, TimerService>()
.AddSingleton<DDNSService>()
.BuildServiceProvider();

View File

@ -1,8 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Threading.Tasks;
using DDNSUpdater.Interfaces;
using DDNSUpdater.Logging;
@ -12,9 +14,9 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Serializers;
using Spectre.Console;
using Console = Spectre.Console.AnsiConsole;
using ContentType = RestSharp.Serializers.ContentType;
namespace DDNSUpdater.Services;
@ -118,7 +120,8 @@ public class DDNSService : IDDNSService
foreach (var domainList in domainDict)
{
try
{
var dyndns = new DynamicDns()
{
Domains = domainList.Value,
@ -133,10 +136,16 @@ public class DDNSService : IDDNSService
request.AddStringBody(content, ContentType.Json);
try
{
var response = client.ExecutePost<DynamicDnsResponse>(request);
if (response.StatusCode == HttpStatusCode.Forbidden)
{
_logger.LogError($"Could not Fetch UpdateURL for {domainList.Key}");
Environment.Exit(7);
}
_logger.LogDebug(response.Data.UpdateUrl);
updateURLs.Add(response.Data.UpdateUrl);
}
catch (Exception error)

View File

@ -3,6 +3,7 @@
"ColorConsole": {
"LogLevelToColorMap": {
"Information": "DarkGreen",
"Debug": "Yellow",
"Warning": "Cyan",
"Error": "Red"
}