From 38686c09b2222aa2e6af7135038d447e9f174b1f Mon Sep 17 00:00:00 2001
From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com>
Date: Fri, 1 Jul 2022 11:57:08 -0700
Subject: [PATCH] fix: Updates more logging to Serilog (#1105)
Adds more serilog logging and cleans up some server files.
---
Projects/Server/Items/Item.cs | 27 +-
Projects/Server/Items/ItemBounds.cs | 84 +-
.../Json/Converters/ClientVersionConverter.cs | 27 +-
.../ClientVersionConverterFactory.cs | 17 +-
.../Server/Json/Converters/FlagsConverter.cs | 229 +-
.../Server/Json/Converters/GuidConverter.cs | 27 +-
.../Json/Converters/GuidConverterFactory.cs | 17 +-
.../Json/Converters/IPEndPointConverter.cs | 27 +-
.../Converters/IPEndPointConverterFactory.cs | 17 +-
.../Server/Json/Converters/MapConverter.cs | 29 +-
.../Json/Converters/MapConverterFactory.cs | 17 +-
.../Json/Converters/Point2DConverter.cs | 147 +-
.../Converters/Point2DConverterFactory.cs | 19 +-
.../Json/Converters/Point3DConverter.cs | 151 +-
.../Converters/Point3DConverterFactory.cs | 19 +-
.../Json/Converters/Rectangle3DConverter.cs | 259 +-
.../Converters/Rectangle3DConverterFactory.cs | 17 +-
.../Json/Converters/TimeSpanConverter.cs | 19 +-
.../Converters/TimeSpanConverterFactory.cs | 17 +-
.../Server/Json/Converters/TypeConverter.cs | 38 +-
.../Json/Converters/TypeConverterFactory.cs | 15 +-
.../Json/Converters/WorldLocationConverter.cs | 285 +-
.../WorldLocationConverterFactory.cs | 17 +-
Projects/Server/Mobiles/Body.cs | 253 +-
Projects/Server/Mobiles/Mobile.cs | 14 +-
Projects/Server/Network/NetState/NetState.cs | 2 +-
.../Packets/OutgoingContainerPackets.cs | 10 +-
Projects/Server/Regions/Region.cs | 12 +-
Projects/Server/Skills.cs | 1 -
Projects/Server/Utilities/Utility.cs | 2682 +++++++++--------
30 files changed, 2261 insertions(+), 2234 deletions(-)
diff --git a/Projects/Server/Items/Item.cs b/Projects/Server/Items/Item.cs
index d6d5d0d07..6515aeca4 100644
--- a/Projects/Server/Items/Item.cs
+++ b/Projects/Server/Items/Item.cs
@@ -687,9 +687,9 @@ namespace Server
if (!Stackable && m_Amount > 1)
{
- Console.WriteLine(
- "Warning: 0x{0:X}: Amount changed for non-stackable item '{2}'. ({1})",
- Serial.Value,
+ logger.Warning(
+ "{Serial}: Amount changed for non-stackable item '{Name}'. ({Amount})",
+ Serial,
m_Amount,
GetType().Name
);
@@ -3164,27 +3164,29 @@ namespace Server
if (item == this)
{
- Console.WriteLine(
- "Warning: Adding item to itself: [0x{0} {1}].AddItem( [0x{2} {3}] )",
+ var customException = new InvalidOperationException("Adding item to itself");
+ logger.Warning(
+ customException,
+ "Adding item to itself: ({Serial1} {Item1}).AddItem({Serial2} {Item2})",
Serial,
GetType().Name,
item.Serial,
item.GetType().Name
);
- Console.WriteLine(new StackTrace());
return;
}
if (IsChildOf(item))
{
- Console.WriteLine(
- "Warning: Adding parent item to child: [0x{0} {1}].AddItem( [0x{2} {3}] )",
+ var customException = new InvalidOperationException("Adding parent item to child");
+ logger.Warning(
+ customException,
+ "Adding parent item to child: [{Serial1} {Item1}].AddItem( [{Serial2} {Item2}] )",
Serial,
GetType().Name,
item.Serial,
item.GetType().Name
);
- Console.WriteLine(new StackTrace());
return;
}
@@ -3270,9 +3272,10 @@ namespace Server
if (m_DeltaQueue.Count > 0)
{
- Utility.PushColor(ConsoleColor.DarkYellow);
- Console.WriteLine("Warning: {0} items left in delta queue after processing.", m_DeltaQueue.Count);
- Utility.PopColor();
+ logger.Warning(
+ "{Count} items left in delta queue after processing.",
+ m_DeltaQueue.Count
+ );
}
}
diff --git a/Projects/Server/Items/ItemBounds.cs b/Projects/Server/Items/ItemBounds.cs
index 4fea6ae67..d2333395b 100644
--- a/Projects/Server/Items/ItemBounds.cs
+++ b/Projects/Server/Items/ItemBounds.cs
@@ -1,44 +1,60 @@
+/*************************************************************************
+ * ModernUO *
+ * Copyright 2019-2022 - ModernUO Development Team *
+ * Email: hi@modernuo.com *
+ * File: ItemBounds.cs *
+ * *
+ * This program is free software: you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation, either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see . *
+ *************************************************************************/
+
using System;
using System.IO;
+using Server.Logging;
-namespace Server
+namespace Server;
+
+public static class ItemBounds
{
- public static class ItemBounds
+ private static readonly ILogger logger = LogFactory.GetLogger(typeof(ItemBounds));
+
+ static ItemBounds()
{
- static ItemBounds()
+ Table = new Rectangle2D[TileData.ItemTable.Length];
+
+ if (!File.Exists("Data/Binary/Bounds.bin"))
{
- Table = new Rectangle2D[TileData.ItemTable.Length];
-
- if (File.Exists("Data/Binary/Bounds.bin"))
- {
- using var fs = new FileStream(
- "Data/Binary/Bounds.bin",
- FileMode.Open,
- FileAccess.Read,
- FileShare.Read
- );
- var bin = new BinaryReader(fs);
-
- var count = Math.Min(Table.Length, (int)(fs.Length / 8));
-
- for (var i = 0; i < count; ++i)
- {
- int xMin = bin.ReadInt16();
- int yMin = bin.ReadInt16();
- int xMax = bin.ReadInt16();
- int yMax = bin.ReadInt16();
-
- Table[i].Set(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
- }
-
- bin.Close();
- }
- else
- {
- Console.WriteLine("Warning: Data/Binary/Bounds.bin does not exist");
- }
+ logger.Error("Data/Binary/Bounds.bin does not exist");
+ return;
}
- public static Rectangle2D[] Table { get; }
+ using var fs = new FileStream(
+ "Data/Binary/Bounds.bin",
+ FileMode.Open,
+ FileAccess.Read,
+ FileShare.Read
+ );
+ var bin = new BinaryReader(fs);
+
+ var count = Math.Min(Table.Length, (int)(fs.Length / 8));
+
+ for (var i = 0; i < count; ++i)
+ {
+ int xMin = bin.ReadInt16();
+ int yMin = bin.ReadInt16();
+ int xMax = bin.ReadInt16();
+ int yMax = bin.ReadInt16();
+
+ Table[i].Set(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
+ }
+
+ bin.Close();
}
+
+ public static Rectangle2D[] Table { get; }
}
diff --git a/Projects/Server/Json/Converters/ClientVersionConverter.cs b/Projects/Server/Json/Converters/ClientVersionConverter.cs
index f5f4b41ee..571881ce3 100644
--- a/Projects/Server/Json/Converters/ClientVersionConverter.cs
+++ b/Projects/Server/Json/Converters/ClientVersionConverter.cs
@@ -17,21 +17,20 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class ClientVersionConverter : JsonConverter
- {
- public override ClientVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- if (reader.TokenType == JsonTokenType.String)
- {
- return new ClientVersion(reader.GetString());
- }
+namespace Server.Json;
- throw new JsonException("Value must be a string");
+public class ClientVersionConverter : JsonConverter
+{
+ public override ClientVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (reader.TokenType == JsonTokenType.String)
+ {
+ return new ClientVersion(reader.GetString());
}
- public override void Write(Utf8JsonWriter writer, ClientVersion value, JsonSerializerOptions options) =>
- writer.WriteStringValue(value.ToString());
+ throw new JsonException("Value must be a string");
}
-}
+
+ public override void Write(Utf8JsonWriter writer, ClientVersion value, JsonSerializerOptions options) =>
+ writer.WriteStringValue(value.ToString());
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/ClientVersionConverterFactory.cs b/Projects/Server/Json/Converters/ClientVersionConverterFactory.cs
index c5a5b6e00..86e8f503e 100644
--- a/Projects/Server/Json/Converters/ClientVersionConverterFactory.cs
+++ b/Projects/Server/Json/Converters/ClientVersionConverterFactory.cs
@@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class ClientVersionConverterFactory : JsonConverterFactory
- {
- public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ClientVersion);
+namespace Server.Json;
- public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
- new ClientVersionConverter();
- }
-}
+public class ClientVersionConverterFactory : JsonConverterFactory
+{
+ public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ClientVersion);
+
+ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
+ new ClientVersionConverter();
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/FlagsConverter.cs b/Projects/Server/Json/Converters/FlagsConverter.cs
index b174e6681..0eea803b9 100644
--- a/Projects/Server/Json/Converters/FlagsConverter.cs
+++ b/Projects/Server/Json/Converters/FlagsConverter.cs
@@ -18,133 +18,132 @@ using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
+namespace Server.Json;
+
+public class FlagsConverter : JsonConverter where T : struct, Enum
{
- public class FlagsConverter : JsonConverter where T : struct, Enum
+ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ var flags = 0ul;
+ var underlyingType = Enum.GetUnderlyingType(typeof(T));
+
+ while (true)
{
- var flags = 0ul;
- var underlyingType = Enum.GetUnderlyingType(typeof(T));
-
- while (true)
+ reader.Read();
+ if (reader.TokenType == JsonTokenType.EndObject)
{
- reader.Read();
- if (reader.TokenType == JsonTokenType.EndObject)
- {
- break;
- }
-
- if (reader.TokenType != JsonTokenType.PropertyName)
- {
- throw new JsonException("Invalid Json structure for Flag object");
- }
-
- var key = reader.GetString();
-
- reader.Read();
-
- if (!reader.GetBoolean() || !Enum.TryParse(key, out var val))
- {
- continue;
- }
-
- flags |= ConvertToUInt64(underlyingType, val);
+ break;
}
- switch (Type.GetTypeCode(underlyingType))
+ if (reader.TokenType != JsonTokenType.PropertyName)
{
- case TypeCode.SByte:
- {
- var num = (sbyte)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.Byte:
- {
- var num = (byte)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.Int16:
- {
- var num = (short)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.UInt16:
- {
- var num = (ushort)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.UInt32:
- {
- var num = (uint)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.Int64:
- {
- var num = (long)flags;
- return Unsafe.As(ref num);
- }
- case TypeCode.UInt64:
- {
- return Unsafe.As(ref flags);
- }
- default:
- {
- var num = (int)flags;
- return Unsafe.As(ref num);
- }
+ throw new JsonException("Invalid Json structure for Flag object");
}
+
+ var key = reader.GetString();
+
+ reader.Read();
+
+ if (!reader.GetBoolean() || !Enum.TryParse(key, out var val))
+ {
+ continue;
+ }
+
+ flags |= ConvertToUInt64(underlyingType, val);
}
- public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
+ switch (Type.GetTypeCode(underlyingType))
{
- writer.WriteStartObject();
- var underlyingType = Enum.GetUnderlyingType(typeof(T));
- var intValue = ConvertToUInt64(underlyingType, value);
-
- foreach (var flagName in Enum.GetNames(typeof(T)))
- {
- var flagValue = Enum.Parse(flagName, false);
- var flag = ConvertToUInt64(underlyingType, flagValue);
-
- // Do not write out multi-bit values. This is a custom behavior
- if (flag > 0 && (flag & (flag - 1)) == 0)
+ case TypeCode.SByte:
{
- writer.WriteBoolean(flagName, (intValue & flag) == flag);
+ var num = (sbyte)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.Byte:
+ {
+ var num = (byte)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.Int16:
+ {
+ var num = (short)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.UInt16:
+ {
+ var num = (ushort)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.UInt32:
+ {
+ var num = (uint)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.Int64:
+ {
+ var num = (long)flags;
+ return Unsafe.As(ref num);
+ }
+ case TypeCode.UInt64:
+ {
+ return Unsafe.As(ref flags);
+ }
+ default:
+ {
+ var num = (int)flags;
+ return Unsafe.As(ref num);
}
- }
-
- writer.WriteEndObject();
}
-
- private static ulong ConvertToUInt64(Type underlyingType, object value) =>
- Type.GetTypeCode(underlyingType) switch
- {
- TypeCode.SByte => (ulong)(sbyte)value,
- TypeCode.Byte => (byte)value,
- TypeCode.Int16 => (ulong)(short)value,
- TypeCode.UInt16 => (ushort)value,
- TypeCode.Int32 => (ulong)(int)value,
- TypeCode.UInt32 => (uint)value,
- TypeCode.Int64 => (ulong)(long)value,
- TypeCode.UInt64 => (ulong)value,
- _ => throw new InvalidOperationException()
- };
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static ulong GetUnderlyingTypeLength(TypeCode typeCode) =>
- typeCode switch
- {
- TypeCode.Byte => 8,
- TypeCode.SByte => 8,
- TypeCode.Int16 => 16,
- TypeCode.UInt16 => 16,
- TypeCode.Char => 16,
- TypeCode.Int32 => 32,
- TypeCode.UInt32 => 32,
- TypeCode.Int64 => 64,
- TypeCode.UInt64 => 64,
- _ => 64
- };
}
-}
+
+ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
+ {
+ writer.WriteStartObject();
+ var underlyingType = Enum.GetUnderlyingType(typeof(T));
+ var intValue = ConvertToUInt64(underlyingType, value);
+
+ foreach (var flagName in Enum.GetNames(typeof(T)))
+ {
+ var flagValue = Enum.Parse(flagName, false);
+ var flag = ConvertToUInt64(underlyingType, flagValue);
+
+ // Do not write out multi-bit values. This is a custom behavior
+ if (flag > 0 && (flag & (flag - 1)) == 0)
+ {
+ writer.WriteBoolean(flagName, (intValue & flag) == flag);
+ }
+ }
+
+ writer.WriteEndObject();
+ }
+
+ private static ulong ConvertToUInt64(Type underlyingType, object value) =>
+ Type.GetTypeCode(underlyingType) switch
+ {
+ TypeCode.SByte => (ulong)(sbyte)value,
+ TypeCode.Byte => (byte)value,
+ TypeCode.Int16 => (ulong)(short)value,
+ TypeCode.UInt16 => (ushort)value,
+ TypeCode.Int32 => (ulong)(int)value,
+ TypeCode.UInt32 => (uint)value,
+ TypeCode.Int64 => (ulong)(long)value,
+ TypeCode.UInt64 => (ulong)value,
+ _ => throw new InvalidOperationException()
+ };
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static ulong GetUnderlyingTypeLength(TypeCode typeCode) =>
+ typeCode switch
+ {
+ TypeCode.Byte => 8,
+ TypeCode.SByte => 8,
+ TypeCode.Int16 => 16,
+ TypeCode.UInt16 => 16,
+ TypeCode.Char => 16,
+ TypeCode.Int32 => 32,
+ TypeCode.UInt32 => 32,
+ TypeCode.Int64 => 64,
+ TypeCode.UInt64 => 64,
+ _ => 64
+ };
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/GuidConverter.cs b/Projects/Server/Json/Converters/GuidConverter.cs
index b6c373094..53034d2d4 100644
--- a/Projects/Server/Json/Converters/GuidConverter.cs
+++ b/Projects/Server/Json/Converters/GuidConverter.cs
@@ -17,21 +17,20 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class GuidConverter : JsonConverter
- {
- public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- if (Guid.TryParse(reader.GetString()!, out var guid))
- {
- return guid;
- }
+namespace Server.Json;
- throw new JsonException("Guid must be in the correct format");
+public class GuidConverter : JsonConverter
+{
+ public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (Guid.TryParse(reader.GetString()!, out var guid))
+ {
+ return guid;
}
- public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
- => writer.WriteStringValue(value.ToString());
+ throw new JsonException("Guid must be in the correct format");
}
-}
+
+ public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
+ => writer.WriteStringValue(value.ToString());
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/GuidConverterFactory.cs b/Projects/Server/Json/Converters/GuidConverterFactory.cs
index e0ba7c4ce..8ec9a5444 100644
--- a/Projects/Server/Json/Converters/GuidConverterFactory.cs
+++ b/Projects/Server/Json/Converters/GuidConverterFactory.cs
@@ -17,13 +17,12 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class GuidConverterFactory : JsonConverterFactory
- {
- public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Guid);
+namespace Server.Json;
- public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
- new GuidConverter();
- }
-}
+public class GuidConverterFactory : JsonConverterFactory
+{
+ public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Guid);
+
+ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
+ new GuidConverter();
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/IPEndPointConverter.cs b/Projects/Server/Json/Converters/IPEndPointConverter.cs
index d6c74d0da..41b4bab66 100644
--- a/Projects/Server/Json/Converters/IPEndPointConverter.cs
+++ b/Projects/Server/Json/Converters/IPEndPointConverter.cs
@@ -18,21 +18,20 @@ using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class IPEndPointConverter : JsonConverter
- {
- public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- if (IPEndPoint.TryParse(reader.GetString()!, out var ipep))
- {
- return ipep;
- }
+namespace Server.Json;
- throw new JsonException("IPEndPoint must be in the correct format");
+public class IPEndPointConverter : JsonConverter
+{
+ public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (IPEndPoint.TryParse(reader.GetString()!, out var ipep))
+ {
+ return ipep;
}
- public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
- => writer.WriteStringValue(value.ToString());
+ throw new JsonException("IPEndPoint must be in the correct format");
}
-}
+
+ public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
+ => writer.WriteStringValue(value.ToString());
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/IPEndPointConverterFactory.cs b/Projects/Server/Json/Converters/IPEndPointConverterFactory.cs
index 3a05cb364..3906cbb4d 100644
--- a/Projects/Server/Json/Converters/IPEndPointConverterFactory.cs
+++ b/Projects/Server/Json/Converters/IPEndPointConverterFactory.cs
@@ -18,13 +18,12 @@ using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class IPEndPointConverterFactory : JsonConverterFactory
- {
- public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(IPEndPoint);
+namespace Server.Json;
- public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
- new IPEndPointConverter();
- }
-}
+public class IPEndPointConverterFactory : JsonConverterFactory
+{
+ public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(IPEndPoint);
+
+ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
+ new IPEndPointConverter();
+}
\ No newline at end of file
diff --git a/Projects/Server/Json/Converters/MapConverter.cs b/Projects/Server/Json/Converters/MapConverter.cs
index 832c366ab..a220e5ba0 100644
--- a/Projects/Server/Json/Converters/MapConverter.cs
+++ b/Projects/Server/Json/Converters/MapConverter.cs
@@ -17,19 +17,18 @@ using System;
using System.Text.Json;
using System.Text.Json.Serialization;
-namespace Server.Json
-{
- public class MapConverter : JsonConverter