Skip to content

Commit

Permalink
improved logging a little bit
Browse files Browse the repository at this point in the history
- log protocol version
- enable debug logs by default for integration tests
- added new latest.log target
  • Loading branch information
psu-de committed Aug 18, 2024
1 parent 8b1653c commit e6e3ef6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Components/MineSharp.Protocol/MinecraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public async Task<bool> Connect(GameState nextState)
return true;
}

Logger.Debug($"Connecting to {ip}:{Port}.");
Logger.Debug($"Connecting to {ip}:{Port} with PVN={Data.Version.Protocol}");

try
{
Expand Down
3 changes: 3 additions & 0 deletions MineSharp.Bot.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MineSharp.Bot.IntegrationTests.Tests;
using MineSharp.Bot.Utils;

LoggingHelper.EnableDebugLogs(true);

await PlayerTests.RunAll();
await WindowTests.RunAll();
Expand Down
2 changes: 1 addition & 1 deletion MineSharp.Bot/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logfile" xsi:type="File" fileName="MineSharpLog.log" />
<target name="logconsole" xsi:type="Console" />
</targets>

Expand Down
4 changes: 3 additions & 1 deletion MineSharp.Bot/Utils/LoggingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public static void EnableDebugLogs(bool trace = false)
{
var configuration = new NLog.Config.LoggingConfiguration();

var logfile = new NLog.Targets.FileTarget("customfile") { FileName = $"{DateTime.Now:dd.MM.yyyy hh:mm:ss}.log" };
var logfile = new NLog.Targets.FileTarget("customfile") { FileName = $"logs/{DateTime.Now:dd.MM.yyyy hh:mm:ss}.log" };
var latestlog = new NLog.Targets.FileTarget("latestfile") { FileName = "logs/latest.log", DeleteOldFileOnStartup = true };
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

var level = trace ? LogLevel.Trace : LogLevel.Debug;
configuration.AddRule(level, LogLevel.Fatal, logfile);
configuration.AddRule(level, LogLevel.Fatal, latestlog);
configuration.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);

LogManager.Configuration = configuration;
Expand Down

0 comments on commit e6e3ef6

Please sign in to comment.