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 Microsoft.Extensions.Options;
using RestSharp; using RestSharp;
static T ParseEnum<T>(string value)
{
return (T) Enum.Parse(typeof(T), value, true);
}
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false); .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(); var configuration = builder.Build();
@ -21,13 +31,20 @@ logConfig.CurrentValue.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed
var serviceProvider = new ServiceCollection() var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(configuration) .AddSingleton<IConfiguration>(configuration)
.AddLogging(logging => logging.AddSpecterConsoleLogger(configuration => .AddLogging(logging =>
{ {
// Replace warning value from appsettings.json of "Cyan"
configuration.LogLevelToColorMap[LogLevel.Warning] = ConsoleColor.DarkCyan; logging.AddSpecterConsoleLogger(configuration =>
// Replace warning value from appsettings.json of "Red" {
configuration.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed; // 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<ITimerService, TimerService>()
.AddSingleton<DDNSService>() .AddSingleton<DDNSService>()
.BuildServiceProvider(); .BuildServiceProvider();

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Mime;
using System.Threading.Tasks; using System.Threading.Tasks;
using DDNSUpdater.Interfaces; using DDNSUpdater.Interfaces;
using DDNSUpdater.Logging; using DDNSUpdater.Logging;
@ -12,9 +14,9 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using RestSharp; using RestSharp;
using RestSharp.Serializers;
using Spectre.Console; using Spectre.Console;
using Console = Spectre.Console.AnsiConsole; using Console = Spectre.Console.AnsiConsole;
using ContentType = RestSharp.Serializers.ContentType;
namespace DDNSUpdater.Services; namespace DDNSUpdater.Services;
@ -118,25 +120,32 @@ public class DDNSService : IDDNSService
foreach (var domainList in domainDict) foreach (var domainList in domainDict)
{ {
var dyndns = new DynamicDns()
{
Domains = domainList.Value,
Description = "My DynamicDns"
};
var content = JsonConvert.SerializeObject(dyndns);
var client = new RestClient("https://api.hosting.ionos.com/dns/v1");
var request = new RestRequest("/dyndns", Method.Post);
request.AddHeader("X-API-Key", domainList.Key);
request.AddStringBody(content, ContentType.Json);
try try
{ {
var dyndns = new DynamicDns()
{
Domains = domainList.Value,
Description = "My DynamicDns"
};
var content = JsonConvert.SerializeObject(dyndns);
var client = new RestClient("https://api.hosting.ionos.com/dns/v1");
var request = new RestRequest("/dyndns", Method.Post);
request.AddHeader("X-API-Key", domainList.Key);
request.AddStringBody(content, ContentType.Json);
var response = client.ExecutePost<DynamicDnsResponse>(request); 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); updateURLs.Add(response.Data.UpdateUrl);
} }
catch (Exception error) catch (Exception error)

View File

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