chore(logging): Migrate some logs to ILogger (#582)

This commit is contained in:
Pedro Pardal 2021-04-22 17:45:49 +02:00 committed by GitHub
parent 421d852991
commit c6cbda6f52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 103 additions and 76 deletions

View file

@ -296,7 +296,7 @@ namespace Server
{
if (_pendingAdd.ContainsKey(entity.Serial))
{
Console.Error.WriteLine("Entity {0} was both pending both deletion and addition after save", entity);
logger.Warning("Entity {0} was both pending both deletion and addition after save", entity);
}
RemoveEntity(entity);
@ -382,15 +382,13 @@ namespace Server
try
{
var watch = Stopwatch.StartNew();
logger.Information("Writing snapshot...");
logger.Information("Writing world save snapshot");
Persistence.WriteSnapshot(tempPath);
watch.Stop();
Utility.PushColor(ConsoleColor.Green);
Console.WriteLine("done ({0:F2} seconds)", watch.Elapsed.TotalSeconds);
Utility.PopColor();
logger.Information("Writing world save snapshot done ({0:F2} seconds)", watch.Elapsed.TotalSeconds);
}
catch (Exception ex)
{
@ -399,9 +397,7 @@ namespace Server
if (exception != null)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("failed");
Utility.PopColor();
logger.Error(exception, "Writing world save snapshot failed.");
Persistence.TraceException(exception);
BroadcastStaff(0x35, true, "Writing world save snapshot failed.");
@ -497,9 +493,7 @@ namespace Server
if (exception == null)
{
var duration = watch.Elapsed.TotalSeconds;
Utility.PushColor(ConsoleColor.Green);
Console.WriteLine("done ({0:F2} seconds)", duration);
Utility.PopColor();
logger.Information("World save completed ({0:F2} seconds)", duration);
// Only broadcast if it took at least 150ms
if (duration >= 0.15)
@ -509,9 +503,7 @@ namespace Server
}
else
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("failed");
Utility.PopColor();
logger.Error(exception, "World save failed");
Persistence.TraceException(exception);
BroadcastStaff(0x35, true, "World save failed.");

View file

@ -1,12 +1,15 @@
using System;
using System.IO;
using System.Net;
using Server.Logging;
using Server.Misc;
namespace Server
{
public static class AccessRestrictions
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(AccessRestrictions));
public static void Initialize()
{
EventSink.SocketConnect += EventSink_SocketConnect;
@ -20,14 +23,14 @@ namespace Server
if (Firewall.IsBlocked(ip))
{
Console.WriteLine("Client: {0}: Firewall blocked connection attempt.", ip);
logger.Information("Client: {0}: Firewall blocked connection attempt.", ip);
e.AllowConnection = false;
return;
}
if (IPLimiter.SocketBlock && !IPLimiter.Verify(ip))
{
Console.WriteLine("Client: {0}: Past IP limit threshold", ip);
logger.Warning("Client: {0}: Past IP limit threshold", ip);
using (var op = new StreamWriter("ipLimits.log", true))
{

View file

@ -4,6 +4,8 @@ using System.IO;
using System.Net;
using Server.Accounting;
using Server.Engines.Help;
using Server.Logging;
using Server.Multis;
using Server.Network;
using Server.Regions;
@ -11,6 +13,8 @@ namespace Server.Misc
{
public static class AccountHandler
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(AccountHandler));
private static int MaxAccountsPerIP;
private static bool AutoAccountCreation;
private static readonly bool RestrictDeletion = !TestCenter.Enabled;
@ -309,7 +313,7 @@ namespace Server.Misc
if (!CanCreate(state.Address))
{
Console.WriteLine(
logger.Information(
"Login: {0}: Account '{1}' not created, ip already has {2} account{3}.",
state,
un,
@ -319,7 +323,7 @@ namespace Server.Misc
return null;
}
Console.WriteLine("Login: {0}: Creating new account '{1}'", state, un);
logger.Information("Login: {0}: Creating new account '{1}'", state, un);
var a = new Account(un, pw);
@ -333,7 +337,7 @@ namespace Server.Misc
e.Accepted = false;
e.RejectReason = ALRReason.InUse;
Console.WriteLine("Login: {0}: Past IP limit threshold", e.State);
logger.Information("Login: {0}: Past IP limit threshold", e.State);
using var op = new StreamWriter("ipLimits.log", true);
op.WriteLine("{0}\tPast IP limit threshold\t{1}", e.State, Core.Now);
@ -361,28 +365,28 @@ namespace Server.Misc
}
else
{
Console.WriteLine("Login: {0}: Invalid username '{1}'", e.State, un);
logger.Information("Login: {0}: Invalid username '{1}'", e.State, un);
e.RejectReason = ALRReason.Invalid;
}
}
else if (!acct.HasAccess(e.State))
{
Console.WriteLine("Login: {0}: Access denied for '{1}'", e.State, un);
logger.Information("Login: {0}: Access denied for '{1}'", e.State, un);
e.RejectReason = LockdownLevel > AccessLevel.Player ? ALRReason.BadComm : ALRReason.BadPass;
}
else if (!acct.CheckPassword(pw))
{
Console.WriteLine("Login: {0}: Invalid password for '{1}'", e.State, un);
logger.Information("Login: {0}: Invalid password for '{1}'", e.State, un);
e.RejectReason = ALRReason.BadPass;
}
else if (acct.Banned)
{
Console.WriteLine("Login: {0}: Banned account '{1}'", e.State, un);
logger.Information("Login: {0}: Banned account '{1}'", e.State, un);
e.RejectReason = ALRReason.Blocked;
}
else
{
Console.WriteLine("Login: {0}: Valid credentials for '{1}'", e.State, un);
logger.Information("Login: {0}: Valid credentials for '{1}'", e.State, un);
e.State.Account = acct;
e.Accepted = true;
@ -401,7 +405,7 @@ namespace Server.Misc
{
e.Accepted = false;
Console.WriteLine("Login: {0}: Past IP limit threshold", e.State);
logger.Warning("Login: {0}: Past IP limit threshold", e.State);
using var op = new StreamWriter("ipLimits.log", true);
op.WriteLine("{0}\tPast IP limit threshold\t{1}", e.State, Core.Now);
@ -418,24 +422,24 @@ namespace Server.Misc
}
else if (!acct.HasAccess(e.State))
{
Console.WriteLine("Login: {0}: Access denied for '{1}'", e.State, un);
logger.Information("Login: {0}: Access denied for '{1}'", e.State, un);
e.Accepted = false;
}
else if (!acct.CheckPassword(pw))
{
Console.WriteLine("Login: {0}: Invalid password for '{1}'", e.State, un);
logger.Information("Login: {0}: Invalid password for '{1}'", e.State, un);
e.Accepted = false;
}
else if (acct.Banned)
{
Console.WriteLine("Login: {0}: Banned account '{1}'", e.State, un);
logger.Information("Login: {0}: Banned account '{1}'", e.State, un);
e.Accepted = false;
}
else
{
acct.LogAccess(e.State);
Console.WriteLine("Login: {0}: Account '{1}' at character list", e.State, un);
logger.Information("Login: {0}: Account '{1}' at character list", e.State, un);
e.State.Account = acct;
e.Accepted = true;
e.CityInfo = StartingCities;

View file

@ -2,11 +2,14 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Server.Logging;
namespace Server.Accounting
{
public static class Accounts
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(Accounts));
private static readonly Dictionary<string, IAccount> _accountsByName = new(32, StringComparer.OrdinalIgnoreCase);
private static Dictionary<Serial, IAccount> _accountsById = new(32);
private static Serial _lastAccount;
@ -111,7 +114,7 @@ namespace Server.Accounting
}
catch
{
Console.WriteLine("Warning: Account instance load failed");
logger.Warning("Account instance load failed");
}
}
}

View file

@ -54,7 +54,6 @@ namespace Server.Commands
}
catch
{
// Console.WriteLine( "Denied" );
}
}
}

View file

@ -19,11 +19,14 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using MimeKit;
using Server.Json;
using Server.Logging;
namespace Server.Configurations
{
public static class EmailConfiguration
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(EmailConfiguration));
private const string m_RelPath = "Configuration/email-settings.json";
public static MailboxAddress CrashAddress { get; private set; }
@ -45,28 +48,21 @@ namespace Server.Configurations
if (File.Exists(path))
{
Console.Write($"Core: Reading email configuration from {m_RelPath}...");
settings = JsonConfig.Deserialize<Settings>(path);
if (settings == null)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("failed");
Utility.PopColor();
logger.Error($"Failed reading email configuration from {m_RelPath}");
throw new JsonException($"Failed to deserialize {path}.");
}
Utility.PushColor(ConsoleColor.Green);
Console.WriteLine("done");
Utility.PopColor();
logger.Information($"Email configuration read from {m_RelPath}");
}
else
{
settings = new Settings();
JsonConfig.Serialize(path, settings);
Utility.PushColor(ConsoleColor.Green);
Console.WriteLine($"Core: Email Configuration saved to {m_RelPath}.");
Utility.PopColor();
logger.Information($"Email configuration saved to {m_RelPath}.");
}
EmailEnabled = settings.enabled;

View file

@ -3,12 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using Server.Gumps;
using Server.Items;
using Server.Logging;
using Server.Utilities;
namespace Server.Engines.MLQuests.Objectives
{
public class DeliverObjective : BaseObjective
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(DeliverObjective));
public DeliverObjective(Type delivery, int amount, TextDefinition name, Type destination, bool spawnsDelivery = true)
{
Delivery = delivery;
@ -23,7 +26,7 @@ namespace Server.Engines.MLQuests.Objectives
if (itemid <= 0 || itemid > 0x4000)
{
Console.WriteLine("Warning: cliloc {0} is likely giving the wrong item ID", name.Number);
logger.Warning("Cliloc {0} is likely giving the wrong item ID", name.Number);
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using Server.Accounting;
using Server.Factions;
using Server.Items;
using Server.Logging;
using Server.Mobiles;
using Server.Network;
@ -9,6 +10,8 @@ namespace Server.Misc
{
public static class CharacterCreation
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(CharacterCreation));
private static readonly TimeSpan BadStartMessageDelay = TimeSpan.FromSeconds(3.5);
private static readonly CityInfo m_NewHavenInfo =
@ -630,7 +633,7 @@ namespace Server.Misc
if (newChar == null)
{
Console.WriteLine("Login: {0}: Character creation failed, account full", state);
logger.Information("Login: {0}: Character creation failed, account full", state);
return;
}
@ -700,9 +703,15 @@ namespace Server.Misc
newChar.MoveToWorld(city.Location, city.Map);
Console.WriteLine("Login: {0}: New character being created (account={1})", state, args.Account.Username);
Console.WriteLine(" - Character: {0} (serial={1})", newChar.Name, newChar.Serial);
Console.WriteLine(" - Started: {0} {1} in {2}", city.City, city.Location, city.Map);
logger.Information(
"Login: {0}: New character being created (account={1}, character={2}, serial={3}, started.city={4}, started.location={5}, started.map={6})",
state,
args.Account.Username,
newChar.Name,
newChar.Serial,
city.City,
city.Location,
city.Map);
new WelcomeTimer(newChar).Start();
}

View file

@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using Server.Items;
using Server.Logging;
using Server.Multis;
namespace Server.Misc
{
public static class Cleanup
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(Cleanup));
public static void Initialize()
{
Timer.DelayCall(TimeSpan.FromSeconds(2.5), Run);
@ -118,7 +121,7 @@ namespace Server.Misc
{
if (boxes > 0)
{
Console.WriteLine(
logger.Information(
"Cleanup: Detected {0} inaccessible items, including {1} bank boxes, removing..",
items.Count,
boxes
@ -126,7 +129,7 @@ namespace Server.Misc
}
else
{
Console.WriteLine("Cleanup: Detected {0} inaccessible items, removing..", items.Count);
logger.Information("Cleanup: Detected {0} inaccessible items, removing..", items.Count);
}
for (var i = 0; i < items.Count; ++i)
@ -137,7 +140,7 @@ namespace Server.Misc
if (hairCleanup.Count > 0)
{
Console.WriteLine(
logger.Information(
"Cleanup: Detected {0} hair and facial hair items being worn, converting to their virtual counterparts..",
hairCleanup.Count
);

View file

@ -2,6 +2,7 @@ using System;
using System.Diagnostics;
using System.IO;
using Server.Gumps;
using Server.Logging;
using Server.Mobiles;
using Server.Network;
@ -9,6 +10,8 @@ namespace Server.Misc
{
public static class ClientVerification
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(ClientVerification));
private static bool m_DetectClientRequirement;
private static OldClientResponse m_OldClientResponse;
@ -65,13 +68,11 @@ namespace Server.Misc
if (Required != null)
{
Utility.PushColor(ConsoleColor.White);
Console.WriteLine(
logger.Information(
"Restricting client version to {0}. Action to be taken: {1}",
Required,
m_OldClientResponse
);
Utility.PopColor();
}
}

View file

@ -2,12 +2,15 @@ using System;
using System.Diagnostics;
using System.IO;
using Server.Accounting;
using Server.Logging;
using Server.Network;
namespace Server.Misc
{
public static class CrashGuard
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(CrashGuard));
private static bool Enabled;
private static bool SaveBackup;
private static bool RestartServer; // Disable this if using a daemon/service
@ -51,25 +54,25 @@ namespace Server.Misc
private static void SendEmail(string filePath)
{
Console.Write("Crash: Sending email...");
logger.Information("Sending crash email");
Email.SendCrashEmail(filePath);
}
private static void Restart(ServerCrashedEventArgs e)
{
Console.Write("Crash: Restarting...");
logger.Information("Restarting");
try
{
Process.Start(Core.Assembly.Location, Core.Arguments);
Console.WriteLine("done");
logger.Information("Restart done");
e.Close = true;
}
catch
{
Console.WriteLine("failed");
logger.Error("Restart failed");
}
}
@ -96,7 +99,7 @@ namespace Server.Misc
private static void Backup()
{
Console.Write("Crash: Backing up...");
logger.Information("Backing up");
try
{
@ -123,17 +126,17 @@ namespace Server.Misc
CopyFile(rootOrigin, rootBackup, "Regions/Regions.bin");
CopyFile(rootOrigin, rootBackup, "Regions/Regions.idx");
Console.WriteLine("done");
logger.Information("Backup done");
}
catch
{
Console.WriteLine("failed");
logger.Error("Backup failed");
}
}
private static void GenerateCrashReport(ServerCrashedEventArgs e)
{
Console.Write("Crash: Generating report...");
logger.Information("Generating crash report");
try
{
@ -210,13 +213,13 @@ namespace Server.Misc
}
}
Console.WriteLine("done");
logger.Information("Crash report generated");
SendEmail(filePath);
}
catch
{
Console.WriteLine("failed");
logger.Error("Crash report generation failed");
}
}
}

View file

@ -4,11 +4,14 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Server.Logging;
namespace Server.Misc
{
public static class ServerList
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(ServerList));
private static IPAddress m_PublicAddress;
/*
* The default setting for Address, a value of 'null', will use your local IP address. If all of your local IP addresses
@ -91,7 +94,7 @@ namespace Server.Misc
}
catch (Exception er)
{
Console.WriteLine(er);
logger.Warning(er, "Unhandled exception at server list");
e.Rejected = true;
}
}
@ -100,16 +103,15 @@ namespace Server.Misc
{
if (!HasPublicIPAddress())
{
Console.Write("ServerList: Auto-detecting public IP address...");
m_PublicAddress = FindPublicAddress();
if (m_PublicAddress != null)
{
Console.WriteLine("done ({0})", m_PublicAddress);
logger.Information("Auto-detected public IP address ({0})", m_PublicAddress);
}
else
{
Console.WriteLine("failed");
logger.Warning("Could not auto-detect public IP address");
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Server.Items;
using Server.Logging;
namespace Server.Multis
{
@ -14,6 +15,8 @@ namespace Server.Multis
public class BaseContestHouse : BaseHouse
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(BaseContestHouse));
public BaseContestHouse(ContestHouseType type, int multiID, Mobile owner, int maxLockDown, int maxSecure)
: base(multiID, owner, maxLockDown, maxSecure)
{
@ -193,11 +196,11 @@ namespace Server.Multis
{
if (value.Count > 2)
{
Console.WriteLine("Warning: More than 2 teleporters detected for {0:X}!", key);
logger.Warning("More than 2 teleporters detected for {0:X}!", key);
}
else if (value.Count <= 1)
{
Console.WriteLine("Warning: 1 or less teleporters detected for {0:X}!", key);
logger.Warning("1 or less teleporters detected for {0:X}!", key);
continue;
}

View file

@ -18,12 +18,15 @@ using System.Buffers;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using Server.Logging;
using Server.Network;
namespace Server.Multis
{
public static class HousePackets
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(HousePackets));
public static void SendBeginHouseCustomization(this NetState ns, Serial house)
{
if (ns == null)
@ -254,7 +257,7 @@ namespace Server.Multis
if (ce != ZlibError.Okay)
{
Console.WriteLine("ZLib error: {0} (#{1})", ce, (int)ce);
logger.Warning("ZLib error: {0} (#{1})", ce, (int)ce);
length = 0;
size = 0;
}

View file

@ -16,12 +16,16 @@
using System;
using System.Buffers;
using Server.Accounting;
using Server.Items;
using Server.Logging;
using Server.Text;
namespace Server.Network
{
public static class ConnectUO
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(ConnectUO));
public enum ConnectUOServerType
{
RunUO,
@ -59,10 +63,7 @@ namespace Server.Network
}
catch
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("ConnectUO token could not be parsed");
Console.WriteLine("Make sure modernuo.json is properly configured");
Utility.PopColor();
logger.Warning("ConnectUO token could not be parsed. Make sure modernuo.json is properly configured");
_token = null;
}

View file

@ -3,13 +3,17 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Server.Json;
using Server.Logging;
using Server.Mobiles;
using Server.Network;
using Server.Utilities;
namespace Server.Regions
{
public class GuardedRegion : BaseRegion
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(GuardedRegion));
private static readonly object[] m_GuardParams = new object[1];
private readonly Dictionary<Mobile, GuardTimer> m_GuardCandidates = new();
@ -31,9 +35,7 @@ namespace Server.Regions
if (!typeof(BaseGuard).IsAssignableFrom(m_GuardType))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid guard type for region '{0}'", this);
Console.ResetColor();
logger.Warning("Invalid guard type for region '{0}'", this);
m_GuardType = DefaultGuardType;
}
}