refactor: logging to loggermessages
All checks were successful
Test if Server can be built / build-server (push) Successful in 23s

This commit is contained in:
Timothy Schenk 2023-10-27 17:40:58 +02:00
parent 1455bdd75a
commit fa52bf66f8
7 changed files with 85 additions and 28 deletions

View file

@ -45,7 +45,11 @@ public class ChannelSession : TcpSession
using (var ms = new MemoryStream(Decrypt(buffer))) using (var ms = new MemoryStream(Decrypt(buffer)))
using (var cs = new CryptoStream(ms, this.decryptor, CryptoStreamMode.Read)) using (var cs = new CryptoStream(ms, this.decryptor, CryptoStreamMode.Read))
{ {
cs.Read(buffer); var amountOfReadBytes = cs.Read(buffer);
if (amountOfReadBytes != buffer.Length)
{
this.logger.LogError("Amount of read bytes is not equal to buffer length.");
}
} }
base.OnReceived(buffer, offset, size); base.OnReceived(buffer, offset, size);

View file

@ -78,7 +78,14 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
IsGameMaster = true IsGameMaster = true
}; };
var sess = session as AuthSession; var sess = session as AuthSession;
if (account != null)
{
if (sess != null)
{
sess.AccountId = account.Id; sess.AccountId = account.Id;
sess.Send(loginResponsePacket); }
}
sess?.Send(loginResponsePacket);
} }
} }

View file

@ -0,0 +1,50 @@
namespace Server;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Packets;
public static partial class PacketLoggerMessages
{
[LoggerMessage(EventId = 0, Level = LogLevel.Information,
Message = "Packet creation function created for {PacketID}")]
public static partial void PacketCreationFunctionCreated(this ILogger logger, OperationCode packetId);
[LoggerMessage(EventId = 1, Level = LogLevel.Trace,
Message = "Packet with ID: {PacketID} has been added as {PacketName}")]
public static partial void PacketWithIdAdded(this ILogger logger, OperationCode packetId, string packetName);
[LoggerMessage(EventId = 2, Level = LogLevel.Trace,
Message = "PacketHandler with ID: {PacketID} has been added as {PacketName}")]
public static partial void PacketHandlerWithIdAdded(this ILogger logger, OperationCode packetId,
[CanBeNull] string packetName);
[LoggerMessage(EventId = 3, Level = LogLevel.Information,
Message = "Packet with ID: {PacketID} has been received")]
public static partial void PacketReceived(this ILogger logger, OperationCode packetId);
[LoggerMessage(EventId = 4, Level = LogLevel.Trace,
Message = "[{SessionID}] Packet with ID: {PacketID} is being dequeued")]
public static partial void PacketDequeued(this ILogger logger, Guid sessionId, OperationCode packetId);
[LoggerMessage(EventId = 5, Level = LogLevel.Information,
Message = "Couldn't find Packet type for Id: {PacketID}")]
public static partial void PacketTypeNotFound(this ILogger logger, OperationCode packetId);
[LoggerMessage(EventId = 6, Level = LogLevel.Trace,
Message = "[{SessionID}] Packet with ID: {PacketID} has finished")]
public static partial void PacketFinished(this ILogger logger, Guid sessionId, OperationCode packetId);
[LoggerMessage(EventId = 7, Level = LogLevel.Critical,
Message = "No PacketHandlers have been found")]
public static partial void NoPacketHandlersFound(this ILogger logger);
[LoggerMessage(EventId = 8, Level = LogLevel.Information,
Message = "Packet data {PacketData}")]
public static partial void PacketData(this ILogger logger, string packetData);
[LoggerMessage(EventId = 9, Level = LogLevel.Critical,
Message = "No Packets have been found")]
public static partial void NoPacketsFound(this ILogger logger);
}

View file

@ -16,8 +16,8 @@ public enum LoginResponseReason : byte
LockedDuePayment, LockedDuePayment,
UserDeletedBigpoint, UserDeletedBigpoint,
NoMoreTries, NoMoreTries,
XMLError12, XmlError12,
XMLRPCError, XmlRpcError,
FileLoadFail, // arbitrary file load error (maybe we're able to load custom files) FileLoadFail, // arbitrary file load error (maybe we're able to load custom files)
ThisIsNotAServiceArea, ThisIsNotAServiceArea,
None None

View file

@ -60,7 +60,7 @@ public class PacketDistributorService : IHostedService
Return(packetVariable); Return(packetVariable);
}).Compile(); }).Compile();
logger.LogInformation("Packet creation function created for {Opcode}", packetsType.Key); logger.PacketCreationFunctionCreated(packetsType.Key);
tempDeserializationMap.Add(packetsType.Key, lambda); tempDeserializationMap.Add(packetsType.Key, lambda);
} }
@ -73,18 +73,20 @@ public class PacketDistributorService : IHostedService
private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly) private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly)
{ {
// ! : We are filtering if types that don't have an instance of the required Attribute
var packetsWithId = executingAssembly.GetTypes().AsParallel() var packetsWithId = executingAssembly.GetTypes().AsParallel()
.Where(type => type.HasInterface(typeof(IPacket)) && !type.IsInterface && !type.IsAbstract) .Where(type => type.HasInterface(typeof(IPacket)) && type is { IsInterface: false, IsAbstract: false })
.Where(type => type.GetCustomAttribute<PacketIdAttribute>() != null) .Select(type => new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute>() })
.ToDictionary(type => type.GetCustomAttribute<PacketIdAttribute>().Code); .Where(item => item.Attribute is not null)
.ToDictionary(item => item.Attribute!.Code, item => item.Type);
if (packetsWithId is not { Count: 0 }) if (packetsWithId is not { Count: 0 })
{ {
packetsWithId.AsParallel().ForAll(packet => this.logger.LogTrace("Packet with ID: {PacketID} has been added as {PacketName}", packet.Key, packetsWithId.AsParallel()
packet.Value.FullName)); .ForAll(packet => this.logger.PacketWithIdAdded(packet.Key, packet.Value.FullName));
return packetsWithId; return packetsWithId;
} }
this.logger.LogCritical("No Packets have been found"); this.logger.NoPacketsFound();
throw new IncompleteInitialization(); throw new IncompleteInitialization();
} }
@ -100,13 +102,12 @@ public class PacketDistributorService : IHostedService
if (packetHandlersWithId is not { Count: 0 }) if (packetHandlersWithId is not { Count: 0 })
{ {
packetHandlersWithId.AsParallel().ForAll(packetHandler => this.logger.LogTrace("PacketHandler with ID: {PacketID} has been added as {PacketName}", packetHandlersWithId.AsParallel().ForAll(packetHandler =>
packetHandler.Key, this.logger.PacketHandlerWithIdAdded(packetHandler.Key, packetHandler.Value.FullName));
packetHandler.Value.FullName));
return packetHandlersWithId; return packetHandlersWithId;
} }
this.logger.LogCritical("No PacketHandlers have been found"); this.logger.NoPacketHandlersFound();
throw new IncompleteInitialization(); throw new IncompleteInitialization();
} }
@ -114,8 +115,7 @@ public class PacketDistributorService : IHostedService
{ {
this.concurrentQueue.Enqueue(rawPacket); this.concurrentQueue.Enqueue(rawPacket);
this.DequeueRawPacket(); this.DequeueRawPacket();
this.logger.LogInformation("Packet with ID: {MessageOperationCode} has been received", this.logger.PacketReceived(rawPacket.OperationCode);
rawPacket.OperationCode);
} }
private void DequeueRawPacket() private void DequeueRawPacket()
@ -128,21 +128,18 @@ public class PacketDistributorService : IHostedService
private void InvokePacketHandler(RawPacket item) private void InvokePacketHandler(RawPacket item)
{ {
this.logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} is being dequeued", this.logger.PacketDequeued(item.Session.Id, item.OperationCode);
item.Session.Id, item.OperationCode);
if (!this.deserializationMap.ContainsKey(item.OperationCode)) if (!this.deserializationMap.ContainsKey(item.OperationCode))
{ {
this.logger.LogInformation("Couldn't find Packet type for Id: {Opcode}", item.OperationCode); this.logger.PacketTypeNotFound(item.OperationCode);
return; return;
} }
var packet = this.deserializationMap[item.OperationCode](item.MessageBody); var packet = this.deserializationMap[item.OperationCode](item.MessageBody);
this.logger.LogInformation("Packet data {PacketData}", JsonConvert.SerializeObject(packet)); this.logger.PacketData(JsonConvert.SerializeObject(packet));
this.packetHandlersInstantiation[item.OperationCode].GetType().GetMethod("HandleAsync") this.packetHandlersInstantiation[item.OperationCode].GetType().GetMethod("HandleAsync")
?.Invoke(this.packetHandlersInstantiation[item.OperationCode], new object[] { packet, item.Session }); ?.Invoke(this.packetHandlersInstantiation[item.OperationCode], new object[] { packet, item.Session });
this.logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} has finished", this.logger.PacketFinished(item.Session.Id, item.OperationCode);
item.Session.Id,
item.OperationCode);
} }
} }

View file

@ -13,7 +13,7 @@ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
# CONFIGURATION # CONFIGURATION
########################################################################### ###########################################################################
$BuildProjectFile = "$PSScriptRoot\build\_build.csproj" $BuildProjectFile = "$PSScriptRoot\build\build.csproj"
$TempDirectory = "$PSScriptRoot\\.nuke\temp" $TempDirectory = "$PSScriptRoot\\.nuke\temp"
$DotNetGlobalFile = "$PSScriptRoot\\global.json" $DotNetGlobalFile = "$PSScriptRoot\\global.json"

View file

@ -8,8 +8,7 @@ SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
########################################################################### ###########################################################################
# CONFIGURATION # CONFIGURATION
########################################################################### ###########################################################################
BUILD_PROJECT_FILE="$SCRIPT_DIR/build/build.csproj"
BUILD_PROJECT_FILE="$SCRIPT_DIR/build/_build.csproj"
TEMP_DIRECTORY="$SCRIPT_DIR//.nuke/temp" TEMP_DIRECTORY="$SCRIPT_DIR//.nuke/temp"
DOTNET_GLOBAL_FILE="$SCRIPT_DIR//global.json" DOTNET_GLOBAL_FILE="$SCRIPT_DIR//global.json"