Integrate NLog for centralized logging, replacing Console calls across CLI tasks. Add LoggingService helper for setup and logger retrieval. Update project dependencies.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="NLog" Version="5.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
using System.Text;
|
||||
using FsToolbox.Cli.Helper;
|
||||
using FsToolbox.Cli.Tasks;
|
||||
using NLog;
|
||||
|
||||
namespace FsToolbox.Cli
|
||||
{
|
||||
public partial class CustomTasks
|
||||
{
|
||||
private static readonly Logger Logger = LoggingService.GetLogger<CustomTasks>();
|
||||
|
||||
#region Public Method CheckAldiMembershipsAsync
|
||||
|
||||
/// <summary>
|
||||
@@ -20,7 +23,7 @@ namespace FsToolbox.Cli
|
||||
var stores = await RegionTasks.GetStoresInRegionAsync(httpClient, 139);
|
||||
var activeAldis = stores.Where(x => x.CooperationStatus == RegionTasks.CooperationStatus.Cooperating && x.Name.Contains("ALDI", StringComparison.CurrentCultureIgnoreCase)).ToList();
|
||||
|
||||
Console.WriteLine("Found " + activeAldis.Count + " active ALDI stores in region Siegen.");
|
||||
Logger.Info("Found {ActiveCount} active ALDI stores in region Siegen.", activeAldis.Count);
|
||||
|
||||
var aldiMembers = new List<AldiMember>();
|
||||
|
||||
@@ -28,7 +31,7 @@ namespace FsToolbox.Cli
|
||||
foreach (var activeAldi in activeAldis)
|
||||
{
|
||||
Thread.Sleep(200);
|
||||
Console.WriteLine("Checking members for store: " + activeAldi.Name);
|
||||
Logger.Info("Checking members for store: {Store}", activeAldi.Name);
|
||||
|
||||
var members = await StoreTasks.GetStoreMembersAsync(httpClient, activeAldi.Id);
|
||||
var activeMembers = members.Where(x => x is { Verified: StoreTasks.VerifiedStatus.Verified, Team_Active: StoreTasks.TeamActiveStatus.Active }).ToList();
|
||||
@@ -43,8 +46,8 @@ namespace FsToolbox.Cli
|
||||
var multipleMemberships = grouped.Where(g => g.Count() > 2).OrderByDescending(g => g.Count()).ToList();
|
||||
|
||||
|
||||
Console.WriteLine($"Users with more than two ALDI store memberships: {multipleMemberships.Count}");
|
||||
Console.WriteLine($"Saving data to file.");
|
||||
Logger.Info("Users with more than two ALDI store memberships: {Count}", multipleMemberships.Count);
|
||||
Logger.Info("Saving data to file.");
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("ALDI Store Memberships Report");
|
||||
@@ -94,26 +97,26 @@ namespace FsToolbox.Cli
|
||||
|
||||
if (toConfirm.Count != 0)
|
||||
{
|
||||
toConfirm.ForEach(x => Console.WriteLine($"Slot found: {x.Name} on {x.Date}"));
|
||||
toConfirm.ForEach(x => Logger.Info("Slot found: {Name} on {Date}", x.Name, x.Date));
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write("No unconfirmed slots found.");
|
||||
Logger.Info("No unconfirmed slots found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Confirm question
|
||||
Console.Write("Confirm all unconfirmed slots? (y/n) (Enter for default): ");
|
||||
Logger.Info("Confirm all unconfirmed slots? (y/n) (Enter for default): ");
|
||||
var input = Console.ReadLine();
|
||||
if (string.IsNullOrEmpty(input) || input.Equals("y", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Console.WriteLine("Confirming unconfirmed slots...");
|
||||
Logger.Info("Confirming unconfirmed slots...");
|
||||
|
||||
// Confirm the unconfirmed slots
|
||||
foreach (var confirmEntry in toConfirm)
|
||||
await StoreTasks.PatchPickupAsync(httpClient, 52170, confirmEntry.Date, confirmEntry.User);
|
||||
|
||||
Console.WriteLine("done.");
|
||||
Logger.Info("Done confirming slots.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
56
Cli/Helper/LoggingService.cs
Normal file
56
Cli/Helper/LoggingService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using NLog;
|
||||
using NLog.Config;
|
||||
using NLog.Targets;
|
||||
|
||||
namespace FsToolbox.Cli.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Central logging setup using NLog with console and file targets.
|
||||
/// </summary>
|
||||
public static class LoggingService
|
||||
{
|
||||
private static bool _initialized;
|
||||
|
||||
/// <summary>
|
||||
/// Configures NLog with console and rolling file targets.
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
if (_initialized) return;
|
||||
|
||||
var config = new LoggingConfiguration();
|
||||
var layout = "${longdate} | ${uppercase:${level}} | ${logger} | ${message}${onexception:inner=${newline}${exception:format=tostring}}";
|
||||
|
||||
var consoleTarget = new ConsoleTarget("console")
|
||||
{
|
||||
Layout = layout
|
||||
};
|
||||
|
||||
var fileTarget = new FileTarget("file")
|
||||
{
|
||||
FileName = "${basedir}/logs/fstool.log",
|
||||
ArchiveEvery = FileArchivePeriod.Day,
|
||||
ArchiveNumbering = ArchiveNumberingMode.Rolling,
|
||||
MaxArchiveFiles = 7,
|
||||
Layout = layout,
|
||||
CreateDirs = true
|
||||
};
|
||||
|
||||
config.AddRule(LogLevel.Info, LogLevel.Fatal, consoleTarget);
|
||||
config.AddRule(LogLevel.Info, LogLevel.Fatal, fileTarget);
|
||||
|
||||
LogManager.Configuration = config;
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a logger for the specified type.
|
||||
/// </summary>
|
||||
public static Logger GetLogger<T>() => LogManager.GetLogger(typeof(T).FullName);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a logger for the specified name.
|
||||
/// </summary>
|
||||
public static Logger GetLogger(string name) => LogManager.GetLogger(name);
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,21 @@ using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FsToolbox.Cli;
|
||||
using FsToolbox.Cli.Helper;
|
||||
using NLog;
|
||||
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
LoggingService.Initialize();
|
||||
var logger = LoggingService.GetLogger("FsToolbox.Cli.Program");
|
||||
|
||||
using var httpClient = new HttpClient();
|
||||
|
||||
// Show menu for the user to choose from the two tasks
|
||||
Console.WriteLine("Choose a task to execute:");
|
||||
Console.WriteLine("1. Check Aldi Memberships");
|
||||
Console.WriteLine("2. Confirm all Unconfirmed Pickups for Lindenberg");
|
||||
Console.Write("Enter the number of the task to execute (or any other key to exit): ");
|
||||
logger.Info("Choose a task to execute:");
|
||||
logger.Info("1. Check Aldi Memberships");
|
||||
logger.Info("2. Confirm all Unconfirmed Pickups for Lindenberg");
|
||||
logger.Info("Enter the number of the task to execute (or any other key to exit): ");
|
||||
var choice = Console.ReadLine();
|
||||
|
||||
switch (choice)
|
||||
@@ -25,6 +30,6 @@ switch (choice)
|
||||
await CustomTasks.ConfirmUnconfirmedPickupsLindenbergAsync(httpClient);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Exiting...");
|
||||
logger.Info("Exiting...");
|
||||
break;
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using FsToolbox.Cli.Helper;
|
||||
using NLog;
|
||||
|
||||
namespace FsToolbox.Cli.Tasks
|
||||
{
|
||||
public static partial class RegionTasks
|
||||
{
|
||||
private static readonly Logger Logger = LoggingService.GetLogger(nameof(RegionTasks));
|
||||
|
||||
#region Public Method GetStoresInRegionAsync
|
||||
|
||||
/// <summary>
|
||||
@@ -25,12 +28,12 @@ namespace FsToolbox.Cli.Tasks
|
||||
// handle unsuccessful response
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
await Console.Error.WriteLineAsync($"Region stores retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
Logger.Error("Region stores retrieval failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
return [];
|
||||
}
|
||||
|
||||
Console.WriteLine($"Stores in region {regionId}:");
|
||||
Console.WriteLine(responseBody);
|
||||
Logger.Info("Stores in region {RegionId}:", regionId);
|
||||
Logger.Info(responseBody);
|
||||
|
||||
var root = JsonNode.Parse(responseBody);
|
||||
if (root == null) return [];
|
||||
|
||||
@@ -2,11 +2,14 @@ using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using FsToolbox.Cli.Helper;
|
||||
using NLog;
|
||||
|
||||
namespace FsToolbox.Cli.Tasks
|
||||
{
|
||||
public static partial class StoreTasks
|
||||
{
|
||||
private static readonly Logger Logger = LoggingService.GetLogger(nameof(StoreTasks));
|
||||
|
||||
#region Public Method GetPickupsAsync
|
||||
|
||||
/// <summary>
|
||||
@@ -24,7 +27,7 @@ namespace FsToolbox.Cli.Tasks
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// handle unsuccessful response
|
||||
if (!response.IsSuccessStatusCode) await Console.Error.WriteLineAsync($"Pickup retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
if (!response.IsSuccessStatusCode) Logger.Error("Pickup retrieval failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
|
||||
var root = JsonNode.Parse(responseBody);
|
||||
if (root == null) return [];
|
||||
@@ -60,7 +63,7 @@ namespace FsToolbox.Cli.Tasks
|
||||
// handle unsuccessful response
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
await Console.Error.WriteLineAsync($"Store members retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
Logger.Error("Store members retrieval failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -103,9 +106,9 @@ namespace FsToolbox.Cli.Tasks
|
||||
|
||||
// handle unsuccessful response
|
||||
if (!response.IsSuccessStatusCode)
|
||||
await Console.Error.WriteLineAsync($"Pickup patch failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
Logger.Error("Pickup patch failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
else
|
||||
Console.WriteLine($"Pickup patch succeeded {fsId} on {pickupDate}");
|
||||
Logger.Info("Pickup patch succeeded {FsId} on {PickupDate}", fsId, pickupDate);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -2,11 +2,14 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Json;
|
||||
using FsToolbox.Cli.Helper;
|
||||
using NLog;
|
||||
|
||||
namespace FsToolbox.Cli.Tasks
|
||||
{
|
||||
public static class UserTasks
|
||||
{
|
||||
private static readonly Logger Logger = LoggingService.GetLogger(nameof(UserTasks));
|
||||
|
||||
/// <summary>
|
||||
/// Performs a login request using configured credentials and optional two-factor authentication.
|
||||
/// </summary>
|
||||
@@ -18,19 +21,19 @@ namespace FsToolbox.Cli.Tasks
|
||||
|
||||
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
||||
{
|
||||
Console.WriteLine("Email and password must be configured in appsettings.json.");
|
||||
Logger.Warn("Email and password must be configured in appsettings.json.");
|
||||
return null;
|
||||
}
|
||||
|
||||
string? authCode = null;
|
||||
if (credentials.TwoFactorEnabled)
|
||||
{
|
||||
Console.Write("Enter 2FA code: ");
|
||||
Logger.Info("Enter 2FA code: ");
|
||||
authCode = Console.ReadLine()?.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(authCode))
|
||||
{
|
||||
Console.WriteLine("A valid 2FA code is required when two-factor authentication is enabled.");
|
||||
Logger.Warn("A valid 2FA code is required when two-factor authentication is enabled.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -58,7 +61,7 @@ namespace FsToolbox.Cli.Tasks
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
await Console.Error.WriteLineAsync($"Login failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
Logger.Error("Login failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -97,13 +100,13 @@ namespace FsToolbox.Cli.Tasks
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
await Console.Error.WriteLineAsync($"Get current user failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
|
||||
Logger.Error("Get current user failed ({Status} {Reason}): {Body}", (int)response.StatusCode, response.ReasonPhrase, responseBody);
|
||||
return;
|
||||
}
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
Console.WriteLine("Current User Info:");
|
||||
Console.WriteLine(content);
|
||||
Logger.Info("Current User Info:");
|
||||
Logger.Info(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user