diff --git a/DDNSUpdater/Program.cs b/DDNSUpdater/Program.cs index c5c6692..9f7ab87 100644 --- a/DDNSUpdater/Program.cs +++ b/DDNSUpdater/Program.cs @@ -8,9 +8,19 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using RestSharp; +static T ParseEnum(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(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(configuration) - .AddLogging(logging => logging.AddSpecterConsoleLogger(configuration => + .AddLogging(logging => { - // Replace warning value from appsettings.json of "Cyan" - configuration.LogLevelToColorMap[LogLevel.Warning] = ConsoleColor.DarkCyan; - // Replace warning value from appsettings.json of "Red" - configuration.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed; - })) + + 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() .AddSingleton() .BuildServiceProvider(); diff --git a/DDNSUpdater/Services/DDNSService.cs b/DDNSUpdater/Services/DDNSService.cs index 10c3e44..9230487 100644 --- a/DDNSUpdater/Services/DDNSService.cs +++ b/DDNSUpdater/Services/DDNSService.cs @@ -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,25 +120,32 @@ public class DDNSService : IDDNSService 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 { + 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(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) diff --git a/DDNSUpdater/appsettings.json b/DDNSUpdater/appsettings.json index 80b7915..e9302ed 100644 --- a/DDNSUpdater/appsettings.json +++ b/DDNSUpdater/appsettings.json @@ -3,6 +3,7 @@ "ColorConsole": { "LogLevelToColorMap": { "Information": "DarkGreen", + "Debug": "Yellow", "Warning": "Cyan", "Error": "Red" }