Skip to content

Commit 835613e

Browse files
committed
Added Logfile and debug messages
1 parent c19c91f commit 835613e

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

DiscordModNotifiyer/Apis/SteamApi.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,24 @@ public async Task UpdateSteamMods()
6565

6666
using (var content = new FormUrlEncodedContent(parameters))
6767
{
68+
string jsonContent = "Content was not parsed";
69+
6870
content.Headers.Clear();
6971
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
7072

7173
try
7274
{
7375
HttpResponseMessage response = await httpClient.PostAsync(Program.STEAM_API_COLLECTION_URL, content);
7476

75-
var model = JsonConvert.DeserializeObject<SteamCollectionModel>(await response.Content.ReadAsStringAsync());
77+
jsonContent = await response.Content.ReadAsStringAsync();
78+
var model = JsonConvert.DeserializeObject<SteamCollectionModel>(jsonContent);
7679
var modIds = model.response.collectiondetails.FirstOrDefault()?.children.Select(x => x.publishedfileid);
7780

7881
await CheckSteamMods(modIds.ToList());
7982
}
8083
catch (Exception e)
8184
{
82-
ConsoleExtensions.Error(e.Message);
85+
ConsoleExtensions.Error(e.Message, Program.Settings.Debug ? $"Json Content: {jsonContent}" : "");
8386
return;
8487
}
8588
}

DiscordModNotifiyer/DiscordModNotifiyer.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<ItemGroup>
2222
<PackageReference Include="discord-webhook-client" Version="3.1.0" />
2323
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
24+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
2425
</ItemGroup>
2526

2627
<ItemGroup>

DiscordModNotifiyer/Extensions/ConsoleExtensions.cs

+33-14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static void ClearConsole()
3838
WriteColor($"[// Check the Collection Id:] {Program.Settings.SteamCollection}", ConsoleColor.DarkGreen);
3939
var ids = Program.Settings.SteamCollection ? Program.Settings.SteamCollectionId.ToString() : String.Join(", ", Program.Settings.SteamModIds.ToArray());
4040
WriteColor($"[// Collection Id or Mod Ids:] {ids}", ConsoleColor.DarkGreen);
41+
WriteColor($"[// Logfile:] {Program.Settings.LogFile}", ConsoleColor.DarkGreen);
4142
WriteColor(@"[//--Options------------------------------------------------------]", ConsoleColor.DarkGreen);
4243
WriteColor($"[// 1:] Execute Refresh", ConsoleColor.DarkGreen);
4344
WriteColor($"[// 2:] Reload settings.json", ConsoleColor.DarkGreen);
@@ -47,17 +48,17 @@ public static void ClearConsole()
4748

4849
if (string.IsNullOrEmpty(Program.Settings.SteamApiKey))
4950
{
50-
WriteColor(@"[//--No Steam API Key---------------------------------------------]", ConsoleColor.DarkRed);
51-
WriteColor($"[//:] Please insert a Steam API Key into the Settings.json", ConsoleColor.DarkRed);
52-
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
51+
WriteColor(@"[//--No Steam API Key---------------------------------------------]", ConsoleColor.DarkRed, true);
52+
WriteColor($"[//:] Please insert a Steam API Key into the Settings.json", ConsoleColor.DarkRed, true);
53+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed, true);
5354
Environment.Exit(1);
5455
}
5556

5657
if (string.IsNullOrEmpty(Program.Settings.SteamApiKey))
5758
{
58-
WriteColor(@"[//--No Discord Web Hook------------------------------------------]", ConsoleColor.DarkRed);
59-
WriteColor($"[//:] Please insert a Discord Web Hook into the Settings.json", ConsoleColor.DarkRed);
60-
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
59+
WriteColor(@"[//--No Discord Web Hook------------------------------------------]", ConsoleColor.DarkRed, true);
60+
WriteColor($"[//:] Please insert a Discord Web Hook into the Settings.json", ConsoleColor.DarkRed, true);
61+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed, true);
6162
Environment.Exit(2);
6263
}
6364
}
@@ -69,30 +70,36 @@ public static void ClearConsole()
6970
/// <param name="exitCode"></param>
7071
public static void CriticalError(string text, int exitCode)
7172
{
72-
WriteColor(@"[//--Critical Error-----------------------------------------------]", ConsoleColor.DarkRed);
73-
WriteColor($"[//:] {text}", ConsoleColor.DarkRed);
74-
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
73+
WriteColor(@"[//--Critical Error-----------------------------------------------]", ConsoleColor.DarkRed, true);
74+
WriteColor($"[//:] {text}", ConsoleColor.DarkRed, true);
75+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed, true);
7576
Environment.Exit(exitCode);
7677
}
7778

7879
/// <summary>
7980
/// Set a error message into the console
8081
/// </summary>
8182
/// <param name="text">Information text</param>
82-
public static void Error(string text)
83+
#nullable enable
84+
public static void Error(string text, string? debug = null)
8385
{
84-
WriteColor(@"[// We got an Error...]", ConsoleColor.DarkRed);
85-
WriteColor($"[// ]{text}", ConsoleColor.DarkRed);
86-
WriteColor(@"[// Continue application...]", ConsoleColor.DarkRed);
86+
WriteColor(@"[// We got an Error...]", ConsoleColor.DarkRed, true);
87+
WriteColor($"[// ]{text}", ConsoleColor.DarkRed, true);
88+
if(debug != null || string.IsNullOrEmpty(debug))
89+
{
90+
WriteColor($"[// ]{debug}", ConsoleColor.DarkYellow, true);
91+
}
92+
WriteColor(@"[// Continue application...]", ConsoleColor.DarkRed, true);
8793
}
94+
#nullable disable
8895

8996
/// <summary>
9097
/// Write some coloring console messages for the user
9198
/// https://stackoverflow.com/questions/2743260/is-it-possible-to-write-to-the-console-in-colour-in-net
9299
/// </summary>
93100
/// <param name="message">Message to write</param>
94101
/// <param name="color">ConsoleColor value of the color</param>
95-
public static void WriteColor(string message, ConsoleColor color)
102+
public static void WriteColor(string message, ConsoleColor color, bool isError = false)
96103
{
97104
var pieces = Regex.Split(message, @"(\[[^\]]*\])");
98105

@@ -111,6 +118,18 @@ public static void WriteColor(string message, ConsoleColor color)
111118
}
112119

113120
Console.WriteLine();
121+
122+
if(Program.LogFile != null)
123+
{
124+
if (isError)
125+
{
126+
Program.LogFile.Error(message);
127+
}
128+
else
129+
{
130+
Program.LogFile.Information(message);
131+
}
132+
}
114133
}
115134
}
116135
}

DiscordModNotifiyer/Models/Settings.cs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace DiscordModNotifiyer.Models
55
public class Settings
66
{
77
public bool Debug { get; set; }
8+
#nullable enable
9+
public string? LogFile { get; set; }
10+
#nullable disable
811
public string SteamApiKey { get; set; }
912
public bool AutomaticRefresh { get; set; }
1013
public int AutomaticRefreshMin { get; set; }

DiscordModNotifiyer/Program.cs

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using DiscordModNotifiyer.Extensions;
33
using DiscordModNotifiyer.Models;
44
using Newtonsoft.Json;
5+
using Serilog;
56
using System;
67
using System.IO;
78

@@ -39,6 +40,11 @@ class Program
3940
/// </summary>
4041
public static Settings Settings;
4142

43+
/// <summary>
44+
/// Logfile class
45+
/// </summary>
46+
public static ILogger LogFile;
47+
4248
/// <summary>
4349
/// Constructor
4450
/// </summary>
@@ -47,6 +53,13 @@ static void Main(string[] args)
4753
{
4854
ReloadSettings();
4955

56+
if(Settings.LogFile != null && !string.IsNullOrEmpty(Settings.LogFile))
57+
{
58+
LogFile = new LoggerConfiguration()
59+
.WriteTo.File(Settings.LogFile)
60+
.CreateLogger();
61+
}
62+
5063
var steamApi = new SteamApi();
5164
steamApi.OnUpdatedModsFound += (sender, e) => _ = DiscordExtensions.SendHook(e.Mods);
5265

DiscordModNotifiyer/Settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"__General": "General settings",
33
"SteamApiKey": "",
4-
"Debug": true,
4+
"Debug": true,
5+
"LogFile": "",
56
"AutomaticRefresh": true,
67
"AutomaticRefreshMin": 1,
78

0 commit comments

Comments
 (0)