Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Fixing logger implementation details.

Co-authored-by: Jakub Dębski <90698026+HEJOK254@users.noreply.github.com>
  • Loading branch information
smellilac and HEJOK254 authored Aug 2, 2024
1 parent e10e963 commit 0079083
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
14 changes: 7 additions & 7 deletions Commands/InteractionServiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static async Task InitAsync()
// So the event has to be used to handle the result
_interactionService.SlashCommandExecuted += OnSlashCommandExecutedAsync;
}
catch
catch (Exception e)
{
Log.Fatal("Error initializing InteractionService");
Log.Fatal($"Error initializing InteractionService: {e.Message}");
throw;
}
}
Expand All @@ -39,7 +39,7 @@ public static async Task RegisterModulesAsync()
// The service might not have been initialized yet
if (_interactionService == null)
{
Log.Error("InteractionService not initialized yet, InteractionServiceManager.RegisterModulesAsync()");
Log.Error("Failed to register modules: InteractionService not initialized.");
throw new InvalidOperationException("InteractionService not initialized while trying to register commands");
}

Expand All @@ -53,11 +53,11 @@ public static async Task RegisterModulesAsync()

await _interactionService.RegisterCommandsGloballyAsync();
_client!.InteractionCreated += OnInteractionCreatedAsync;
Log.Information("Modules registered successfully, InteractionServiceManager");
Log.Information("Modules registered successfully");
}
catch (Exception e)
{
Log.Fatal($"Error registering modules. ({e})");
Log.Fatal($"Error registering modules: {(Program.config != null && Program.config.debug ? e : e.Message)}");
throw;
}
}
Expand All @@ -67,7 +67,7 @@ public static async Task OnInteractionCreatedAsync(SocketInteraction interaction
// The service might not have been initialized yet
if (_interactionService == null)
{
Log.Error("InteractionService not initialized yet, InteractionServiceManager.OnInteractionCreatedAsync()");
Log.Error("Error handling interaction: InteractionService not initialized.");
return;
}

Expand All @@ -80,7 +80,7 @@ public static async Task OnInteractionCreatedAsync(SocketInteraction interaction
}
catch (Exception e)
{
Log.Error($"Error handling interaction. {e.Message}");
Log.Error($"Error handling interaction: {e.Message}");

if (interaction.Type is InteractionType.ApplicationCommand)
{
Expand Down
2 changes: 1 addition & 1 deletion Commands/Modules/VideoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task TrimVideoAsync(
if (!Directory.Exists("./tmp"))
{
Directory.CreateDirectory("./tmp");
Log.Information("TMP directory not found. Created it automatically, VideoUtils");
Log.Information("TMP directory not found. Created it automatically");
}

await DownloadVideoAsync(video.Url, videoInputPath);
Expand Down
6 changes: 3 additions & 3 deletions ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public class Config
string path = "./config.json";
if (!File.Exists(path))
{
Log.Fatal("Config file not found at: {Path}", path);
Log.Fatal($"Config file not found at: {Path.GetFullPath(path)}");
return null;
}

try
{
return JsonConvert.DeserializeObject<Config>(File.ReadAllText(path))!;
}
catch
catch (Exception e)
{
Log.Fatal("Failed to parse config file.");
Log.Fatal($"Failed to parse config file: {e.Message}");
return null;
}
}
Expand Down
1 change: 1 addition & 0 deletions LoggerConfig/LoggerImplementation/AutoLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace QuickEdit.LoggerConfig.LoggerImplementation;

public class AutoLog
{
// LogMessage does not run asynchronously, so we can ignore the DeepSource error
// skipcq: CS-R1073
public static Task LogMessage(LogMessage message)
{
Expand Down
17 changes: 11 additions & 6 deletions LoggerConfig/SerilogConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ public static void ConfigureLogger()

var logPath = Path.Combine(intermediateOutputPath, $"consoleapplog-{DateTime.UtcNow:yyyyMMddHHmmss}.txt");

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.WithMachineName()
.WriteTo.Console()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day)
.CreateLogger();
var loggerConfig = new LoggerConfiguration()
.Enrich.WithMachineName()
.WriteTo.Console()
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day);

if (Program.config != null && Program.config.debug)
loggerConfig.MinimumLevel.Debug();
else
loggerConfig.MinimumLevel.Information();

Log.Logger = loggerConfig.CreateLogger();
}
}

0 comments on commit 0079083

Please sign in to comment.