feat(logging): Adds support for Serilog (#573)

Example:
```cs
public class SomeClass
{
  private static ILogger _logger;
  private static Logger => _logger ??= LogFactory.GetLogger(typeof(SomeClass));
  ...
}
```
This commit is contained in:
Pedro Pardal 2021-04-20 08:43:54 +02:00 • committed by GitHub
parent 5e1ec5416d
commit 3a619e0103
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 347 additions and 176 deletions

View file

@ -17,7 +17,6 @@ using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
@ -28,6 +27,7 @@ using Server.Diagnostics;
using Server.Gumps;
using Server.HuePickers;
using Server.Items;
using Server.Logging;
using Server.Menus;
namespace Server.Network
@ -39,6 +39,8 @@ namespace Server.Network
public partial class NetState : IComparable<NetState>
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(NetState));
private const int RecvPipeSize = 1024 * 64;
private const int SendPipeSize = 1024 * 256;
private static int GumpCap = 512;
@ -343,15 +345,15 @@ namespace Server.Network
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteConsole(string text)
public void LogInfo(string text)
{
Console.WriteLine("Client: {0}: {1}", this, text);
logger.Information("Client: {0}: {1}", this, text);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteConsole(string format, params object[] args)
public void LogInfo(string format, params object[] args)
{
WriteConsole(string.Format(format, args));
LogInfo(string.Format(format, args));
}
public void AddMenu(IMenu menu)
@ -364,7 +366,7 @@ namespace Server.Network
}
else
{
WriteConsole("Exceeded menu cap, disconnecting...");
LogInfo("Exceeded menu cap, disconnecting...");
Disconnect("Exceeded menu cap.");
}
}
@ -394,7 +396,7 @@ namespace Server.Network
}
else
{
WriteConsole("Exceeded hue picker cap, disconnecting...");
LogInfo("Exceeded hue picker cap, disconnecting...");
Disconnect("Exceeded hue picker cap.");
}
}
@ -424,7 +426,7 @@ namespace Server.Network
}
else
{
WriteConsole("Exceeded gump cap, disconnecting...");
LogInfo("Exceeded gump cap, disconnecting...");
Disconnect("Exceeded gump cap.");
}
}
@ -622,7 +624,7 @@ namespace Server.Network
{
if (packetId != 0xCF && packetId != 0x80)
{
WriteConsole("Possible encrypted client detected, disconnecting...");
LogInfo("Possible encrypted client detected, disconnecting...");
HandleError(packetId, packetLength);
return true;
}
@ -740,7 +742,7 @@ namespace Server.Network
PacketHandler handler = GetHandler(packetId);
if (handler == null)
{
WriteConsole($"received unknown packet 0x{packetId:X2} while in state {_protocolState}");
LogInfo($"received unknown packet 0x{packetId:X2} while in state {_protocolState}");
packetLength = length;
return ParserState.Error;
}
@ -771,7 +773,7 @@ namespace Server.Network
{
if (Mobile == null)
{
WriteConsole($"received packet 0x{packetId:X2} before having been attached to a mobile");
LogInfo($"received packet 0x{packetId:X2} before having been attached to a mobile");
return ParserState.Error;
}
@ -1015,7 +1017,7 @@ namespace Server.Network
{
if (Connection != null && _nextActivityCheck - curTicks < 0)
{
WriteConsole("Disconnecting due to inactivity...");
LogInfo("Disconnecting due to inactivity...");
Disconnect("Disconnecting due to inactivity.");
}
}
@ -1147,11 +1149,11 @@ namespace Server.Network
if (a != null)
{
WriteConsole("Disconnected. [{0} Online] [{1}]", count, a);
LogInfo("Disconnected. [{0} Online] [{1}]", count, a);
}
else
{
WriteConsole("Disconnected. [{0} Online]", count);
LogInfo("Disconnected. [{0} Online]", count);
}
}
}

View file

@ -157,7 +157,7 @@ namespace Server.Network
if (check != null && check.Map != Map.Internal)
{
state.WriteConsole("Account in use");
state.LogInfo("Account in use");
state.SendPopupMessage(PMMessage.CharInWorld);
return;
}
@ -274,7 +274,7 @@ namespace Server.Network
if (check != null && check.Map != Map.Internal && check != m)
{
state.WriteConsole("Account in use");
state.LogInfo("Account in use");
state.SendPopupMessage(PMMessage.CharInWorld);
return;
}
@ -395,13 +395,13 @@ namespace Server.Network
if (!m_AuthIDWindow.TryGetValue(authID, out var ap))
{
state.WriteConsole("Invalid client detected, disconnecting...");
state.LogInfo("Invalid client detected, disconnecting...");
state.Disconnect("Unable to find auth id.");
}
if (state._authId != 0 && authID != state._authId || state._authId == 0 && authID != state._seed)
{
state.WriteConsole("Invalid client detected, disconnecting...");
state.LogInfo("Invalid client detected, disconnecting...");
state.Disconnect("Invalid auth id in game login packet.");
return;
}
@ -461,7 +461,7 @@ namespace Server.Network
if (state._seed == 0)
{
state.WriteConsole("Invalid client detected, disconnecting");
state.LogInfo("Invalid client detected, disconnecting");
state.Disconnect("Duplicate seed sent.");
return;
}

View file

@ -120,7 +120,7 @@ namespace Server.Network
{
if (state.Mobile == null)
{
state.WriteConsole(
state.LogInfo(
"Sent in-game packet (0xBFx{0:X2}) before having been attached to a mobile",
packetId
);

View file

@ -203,7 +203,7 @@ namespace Server.Network
}
default:
{
state.WriteConsole("Unknown text-command type 0x{0:X2}: {1}", state, type, command);
state.LogInfo("Unknown text-command type 0x{0:X2}: {1}", state, type, command);
break;
}
}
@ -373,7 +373,7 @@ namespace Server.Network
if (!buttonExists)
{
state.WriteConsole("Invalid gump response, disconnecting...");
state.LogInfo("Invalid gump response, disconnecting...");
var exception = new InvalidGumpResponseException($"Button {buttonID} doesn't exist");
exception.SetStackTrace(new StackTrace());
NetState.TraceException(exception);
@ -387,7 +387,7 @@ namespace Server.Network
if (switchCount < 0 || switchCount > gump.m_Switches)
{
state.WriteConsole("Invalid gump response, disconnecting...");
state.LogInfo("Invalid gump response, disconnecting...");
var exception = new InvalidGumpResponseException($"Bad switch count {switchCount}");
exception.SetStackTrace(new StackTrace());
NetState.TraceException(exception);
@ -408,7 +408,7 @@ namespace Server.Network
if (textCount < 0 || textCount > gump.m_TextEntries)
{
state.WriteConsole("Invalid gump response, disconnecting...");
state.LogInfo("Invalid gump response, disconnecting...");
var exception = new InvalidGumpResponseException($"Bad text entry count {textCount}");
exception.SetStackTrace(new StackTrace());
NetState.TraceException(exception);
@ -427,7 +427,7 @@ namespace Server.Network
if (textLength > 239)
{
state.WriteConsole("Invalid gump response, disconnecting...");
state.LogInfo("Invalid gump response, disconnecting...");
var exception = new InvalidGumpResponseException($"Text entry {i} is too long ({textLength})");
exception.SetStackTrace(new StackTrace());
NetState.TraceException(exception);
@ -614,7 +614,7 @@ namespace Server.Network
if (ph.Ingame && state.Mobile == null)
{
state.WriteConsole(
state.LogInfo(
"Sent in-game packet (0xD7x{0:X2}) before being attached to a mobile",
packetId
);

View file

@ -20,11 +20,14 @@ using System.IO.Compression;
using System.Runtime.CompilerServices;
using Server.Collections;
using Server.Gumps;
using Server.Logging;
namespace Server.Network
{
public static class OutgoingGumpPackets
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(OutgoingGumpPackets));
public static void SendCloseGump(this NetState ns, int typeId, int buttonId)
{
if (ns == null)
@ -73,9 +76,7 @@ namespace Server.Network
if (error != ZlibError.Okay)
{
Utility.PushColor(ConsoleColor.Red);
Core.WriteConsoleLine($"Gump compression failed {error}");
Utility.PopColor();
logger.Warning($"Gump compression failed {error}");
writer.Write(4);
writer.Write(0);

View file

@ -20,11 +20,14 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Server.Logging;
namespace Server.Network
{
public static class TcpServer
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(TcpServer));
private const int MaxConnectionsPerLoop = 250;
// Sanity. 256 * 1024 * 4096 = ~1.3GB of ram
@ -75,7 +78,7 @@ namespace Server.Network
foreach (var ipep in listeningAddresses)
{
Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port);
logger.Information("Listening: {0}:{1}", ipep.Address, ipep.Port);
}
ListeningAddresses = listeningAddresses.ToArray();
@ -104,17 +107,16 @@ namespace Server.Network
// WSAEADDRINUSE
if (se.ErrorCode == 10048)
{
Console.WriteLine("Listener: {0}:{1}: Failed (In Use)", ipep.Address, ipep.Port);
logger.Warning("Listener: {0}:{1}: Failed (In Use)", ipep.Address, ipep.Port);
}
// WSAEADDRNOTAVAIL
else if (se.ErrorCode == 10049)
{
Console.WriteLine("Listener {0}:{1}: Failed (Unavailable)", ipep.Address, ipep.Port);
logger.Warning("Listener {0}:{1}: Failed (Unavailable)", ipep.Address, ipep.Port);
}
else
{
Console.WriteLine("Listener Exception:");
Console.WriteLine(se);
logger.Warning(se, "Listener Exception:");
}
}
@ -128,7 +130,7 @@ namespace Server.Network
while (++count <= MaxConnectionsPerLoop && _connectedQueue.TryDequeue(out var ns))
{
Instances.Add(ns);
ns.WriteConsole("Connected. [{0} Online]", Instances.Count);
ns.LogInfo("Connected. [{0} Online]", Instances.Count);
ns.Start();
}
@ -154,7 +156,7 @@ namespace Server.Network
if (socket.RemoteEndPoint is IPEndPoint ipep)
{
var ip = ipep.Address.ToString();
Console.WriteLine("Listener {0}: Failed (Maximum connections reached)", ip);
logger.Warning("Listener {0}: Failed (Maximum connections reached)", ip);
NetState.TraceDisconnect("Maximum connections reached.", ip);
}