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:
Andre Beging
2025-12-12 09:29:31 +01:00
parent c9a56abe8b
commit 516fceb1dc
7 changed files with 102 additions and 28 deletions

View File

@@ -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);
}
}
}