Skip to content

Commit

Permalink
More Logging / Seq stuff
Browse files Browse the repository at this point in the history
- added topics to library logging, i'm hoping this doesnt affect others using those assemblies
- added fluent seq entity builders
  • Loading branch information
Sichii committed Aug 9, 2023
1 parent 00ee766 commit 21c9382
Show file tree
Hide file tree
Showing 39 changed files with 843 additions and 542 deletions.
29 changes: 20 additions & 9 deletions Chaos.Messaging/ChannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Chaos.Extensions.Common;
using Chaos.IO.Memory;
using Chaos.Messaging.Abstractions;
using Chaos.NLog.Logging.Definitions;
using Chaos.NLog.Logging.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -62,7 +64,9 @@ public void JoinChannel(IChannelSubscriber subscriber, string channelName, bool

if (channelDetails.AddSubscriber(subscriber))
{
Logger.LogDebug("{@SubscriberName} has joined channel {@ChannelName}", subscriber.Name, channelName);
Logger.WithTopics(Topics.Entities.Channel, Topics.Actions.Join)
.WithProperty(subscriber)
.LogDebug("{@SubscriberName} has joined channel {@ChannelName}", subscriber.Name, channelName);

subscriber.SendMessage($"You have joined channel {channelDetails.ChannelNameOverride ?? channelName}");
} else
Expand All @@ -81,7 +85,9 @@ public void LeaveChannel(IChannelSubscriber subscriber, string channelName)

if (channelDetails.RemoveSubscriber(subscriber))
{
Logger.LogDebug("{@SubscriberName} has left channel {@ChannelName}", subscriber.Name, channelName);
Logger.WithTopics(Topics.Entities.Channel, Topics.Actions.Leave)
.WithProperty(subscriber)
.LogDebug("{@SubscriberName} has left channel {@ChannelName}", subscriber.Name, channelName);

subscriber.SendMessage($"You have left channel {channelDetails.ChannelNameOverride ?? channelName}");
} else
Expand Down Expand Up @@ -140,7 +146,9 @@ public void RegisterChannel(
}

Channels.TryAdd(channelName, new ChannelDetails(defaultMessageColor, sendMessageAction, channelNameOverride));
Logger.LogInformation("Channel {@ChannelName} has been registered", channelName);

Logger.WithTopics(Topics.Entities.Channel, Topics.Actions.Create)
.LogInformation("Channel {@ChannelName} has been registered", channelName);

if (subscriber is not null)
JoinChannel(subscriber, channelName, bypassValidation);
Expand Down Expand Up @@ -190,11 +198,13 @@ public void SendMessage(IChannelSubscriber subscriber, string channelName, strin

var defaultMessage = Encoding.Default.GetString(buffer);

Logger.LogInformation(
"Subscriber {@SubscriberName} sent message {@Message} to channel {@ChannelName}",
subscriber.Name,
message,
channelName);
Logger.WithTopics(Topics.Entities.Channel, Topics.Entities.Message, Topics.Actions.Send)
.WithProperty(subscriber)
.LogInformation(
"Subscriber {@SubscriberName} sent message {@Message} to channel {@ChannelName}",
subscriber.Name,
message,
channelName);

foreach (var subDetails in channelDetails.Subscribers.Values)
{
Expand Down Expand Up @@ -239,7 +249,8 @@ public bool UnregisterChannel(string channelName)
foreach (var subDetails in channel.Subscribers.Values)
LeaveChannel(subDetails.Subscriber, channelName);

Logger.LogInformation("Channel {@ChannelName} has been unregistered", channelName);
Logger.WithTopics(Topics.Entities.Channel, Topics.Actions.Delete)
.LogInformation("Channel {@ChannelName} has been unregistered", channelName);

return Channels.TryRemove(channelName, out _);
}
Expand Down
1 change: 1 addition & 0 deletions Chaos.Messaging/Chaos.Messaging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ProjectReference Include="..\Chaos.IO\Chaos.IO.csproj"/>
<ProjectReference Include="..\Chaos.Messaging.Abstractions\Chaos.Messaging.Abstractions.csproj"/>
<ProjectReference Include="..\Chaos.Extensions.Common\Chaos.Extensions.Common.csproj"/>
<ProjectReference Include="..\Chaos.NLog.Logging\Chaos.NLog.Logging.csproj"/>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Chaos.Messaging.Tests"/>
Expand Down
47 changes: 29 additions & 18 deletions Chaos.Messaging/CommandInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Chaos.Common.Definitions;
using Chaos.Extensions.Common;
using Chaos.Messaging.Abstractions;
using Chaos.NLog.Logging.Definitions;
using Chaos.NLog.Logging.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -72,15 +74,19 @@ public async ValueTask HandleCommandAsync(T source, string commandStr)
if (!Commands.TryGetValue(commandName, out var descriptor))
return;

Logger.LogDebug("Handling command {@CommandStr}", commandStr);
Logger.WithTopics(Topics.Entities.Command, Topics.Actions.Execute)
.WithProperty(source)
.LogDebug("Handling command {@CommandStr}", commandStr);

if (descriptor.Details.RequiresAdmin && !source.IsAdmin)
{
Logger.LogWarning(
"Non-Admin {@SourceType} {@SourceName} tried to execute admin command {@CommandStr}",
source.GetType().Name,
source.Name,
commandStr);
Logger.WithTopics(Topics.Entities.Command, Topics.Actions.Execute)
.WithProperty(source)
.LogWarning(
"Non-Admin {@SourceType} {@SourceName} tried to execute admin command {@CommandStr}",
source.GetType().Name,
source.Name,
commandStr);

return;
}
Expand All @@ -89,7 +95,8 @@ public async ValueTask HandleCommandAsync(T source, string commandStr)
{
var commandInstance = (ICommand<T>)ActivatorUtilities.CreateInstance(ServiceProvider, descriptor.Type);

Logger.LogTrace("Successfully created command {@CommandName}", commandName);
Logger.WithTopics(Topics.Entities.Command, Topics.Actions.Create)
.LogTrace("Successfully created command {@CommandName}", commandName);

if (commandName.EqualsI("help") || commandName.EqualsI("commands"))
{
Expand All @@ -103,22 +110,26 @@ async ValueTask InnerExecute()
{
await commandInstance.ExecuteAsync(source, new ArgumentCollection(commandArgs));

Logger.LogInformation(
"{@SourceType} {@SourceName} executed {@CommandStr}",
source.GetType().Name,
source.Name,
commandStr);
Logger.WithTopics(Topics.Entities.Command, Topics.Actions.Execute)
.WithProperty(source)
.LogInformation(
"{@SourceType} {@SourceName} executed {@CommandStr}",
source.GetType().Name,
source.Name,
commandStr);
}

await InnerExecute();
} catch (Exception e)
{
Logger.LogError(
e,
"{@SourceType} {@SourceName} failed to execute {@Command}",
source.GetType().Name,
source.Name,
commandStr);
Logger.WithTopics(Topics.Entities.Command, Topics.Actions.Execute)
.WithProperty(source)
.LogError(
e,
"{@SourceType} {@SourceName} failed to execute {@Command}",
source.GetType().Name,
source.Name,
commandStr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Chaos.NLog.Logging/Chaos.NLog.Logging.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions"/>
</ItemGroup>

Expand Down
89 changes: 39 additions & 50 deletions Chaos.NLog.Logging/Definitions/Topics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,62 @@ public static class Topics
{
public static class Actions
{
#region Bank Actions
public static string Deposit => nameof(Deposit);
public static string Withdraw => nameof(Withdraw);
#endregion

#region Shop Actions
public static string Accepted => nameof(Accepted);
public static string Add => nameof(Add);
public static string Admit => nameof(Admit);
public static string Buy => nameof(Buy);
public static string Sell => nameof(Sell);
#endregion

#region Exchange Actions
public static string Canceled => nameof(Canceled);
public static string Accepted => nameof(Accepted);
#endregion

#region Guild / Group Actions
public static string Promote => nameof(Promote);
public static string Connect => nameof(Connect);
public static string Create => nameof(Create);
public static string Death => nameof(Death);
public static string Delete => nameof(Delete);
public static string Demote => nameof(Demote);
public static string Kick => nameof(Kick);
public static string Admit => nameof(Admit);
public static string Invite => nameof(Invite);
public static string Leave => nameof(Leave);
public static string Deposit => nameof(Deposit);
public static string Disband => nameof(Disband);
#endregion

#region Creature Actions
public static string Walk => nameof(Walk);
public static string Traverse => nameof(Traverse);
public static string Death => nameof(Death);
public static string Disconnect => nameof(Disconnect);
public static string Drop => nameof(Drop);
public static string Execute => nameof(Execute);
public static string Forget => nameof(Forget);
public static string Highlight => nameof(Highlight);
public static string Invite => nameof(Invite);
public static string Join => nameof(Join);
public static string Kick => nameof(Kick);
public static string Learn => nameof(Learn);
public static string Leave => nameof(Leave);
public static string Listening => nameof(Listening);
public static string Load => nameof(Load);
public static string Login => nameof(Login);
public static string Logout => nameof(Logout);
public static string Penalty => nameof(Penalty);
public static string Pickup => nameof(Pickup);
public static string Reward => nameof(Reward);
public static string Message => nameof(Message);
public static string Command => nameof(Command);
public static string Learn => nameof(Learn);
public static string Forget => nameof(Forget);
#endregion

#region Storage Actions
public static string Load => nameof(Load);
public static string Reload => nameof(Reload);
public static string Save => nameof(Save);
public static string Add => nameof(Add);
public static string Remove => nameof(Remove);
public static string Create => nameof(Create);
public static string Read => nameof(Read);
public static string Update => nameof(Update);
public static string Delete => nameof(Delete);
public static string Highlight => nameof(Highlight);
#endregion

#region Server Actions
public static string Processing => nameof(Processing);
public static string Promote => nameof(Promote);
public static string Read => nameof(Read);
public static string Receive => nameof(Receive);
public static string Send => nameof(Send);
public static string Redirect => nameof(Redirect);
public static string Connect => nameof(Connect);
public static string Disconnect => nameof(Disconnect);
public static string Reload => nameof(Reload);
public static string Remove => nameof(Remove);
public static string Reward => nameof(Reward);
public static string Save => nameof(Save);
public static string Sell => nameof(Sell);
public static string Send => nameof(Send);
public static string Traverse => nameof(Traverse);
public static string Update => nameof(Update);
public static string Validation => nameof(Validation);
#endregion
public static string Walk => nameof(Walk);
public static string Withdraw => nameof(Withdraw);
}

public static class Entities
{
public static string Aisling => nameof(Aisling);
public static string Backup => nameof(Backup);
public static string BulletinBoard => nameof(BulletinBoard);
public static string Channel => nameof(Channel);
public static string Client => nameof(Client);
public static string Command => nameof(Command);
public static string Creature => nameof(Creature);
public static string DeltaMonitor => nameof(DeltaMonitor);
public static string Dialog => nameof(Dialog);
public static string Effect => nameof(Effect);
public static string Exchange => nameof(Exchange);
Expand All @@ -89,11 +75,14 @@ public static class Entities
public static string MapInstance => nameof(MapInstance);
public static string MapTemplate => nameof(MapTemplate);
public static string Merchant => nameof(Merchant);
public static string Message => nameof(Message);
public static string MetaData => nameof(MetaData);
public static string Monster => nameof(Monster);
public static string Options => nameof(Options);
public static string Packet => nameof(Packet);
public static string Post => nameof(Post);
public static string Quest => nameof(Quest);
public static string Script => nameof(Script);
public static string Skill => nameof(Skill);
public static string Spell => nameof(Spell);
public static string WorldMap => nameof(WorldMap);
Expand Down
22 changes: 14 additions & 8 deletions Chaos.Networking/Abstractions/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Chaos.Extensions.Common;
using Chaos.Networking.Entities.Client;
using Chaos.Networking.Options;
using Chaos.NLog.Logging.Definitions;
using Chaos.NLog.Logging.Extensions;
using Chaos.Packets;
using Chaos.Packets.Abstractions;
using Chaos.Packets.Abstractions.Definitions;
Expand Down Expand Up @@ -97,7 +99,9 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Socket.Bind(endPoint);
Socket.Listen(20);
Socket.BeginAccept(OnConnection, Socket);
Logger.LogInformation("Listening on {@EndPoint}", endPoint.ToString());

Logger.WithTopics(Topics.Actions.Listening)
.LogInformation("Listening on {@EndPoint}", endPoint.ToString());

await stoppingToken.WaitTillCanceled();

Expand Down Expand Up @@ -165,12 +169,13 @@ public virtual async ValueTask ExecuteHandler<TArgs>(T client, TArgs args, Func<
await action(client, args);
} catch (Exception e)
{
Logger.LogError(
e,
"{@ClientType} failed to execute inner handler with args type {@ArgsType} ({@Args})",
client.GetType().Name,
args!.GetType().Name,
args);
Logger.WithTopics(Topics.Entities.Packet, Topics.Actions.Processing)
.LogError(
e,
"{@ClientType} failed to execute inner handler with args type {@ArgsType} ({@Args})",
client.GetType().Name,
args!.GetType().Name,
args);
}
}

Expand All @@ -188,7 +193,8 @@ public virtual async ValueTask ExecuteHandler(T client, Func<T, ValueTask> actio
await action(client);
} catch (Exception e)
{
Logger.LogError(e, "{@ClientType} failed to execute inner handler", client.GetType().Name);
Logger.WithTopics(Topics.Entities.Packet, Topics.Actions.Processing)
.LogError(e, "{@ClientType} failed to execute inner handler", client.GetType().Name);
}
}

Expand Down
28 changes: 19 additions & 9 deletions Chaos.Networking/Abstractions/SocketClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Chaos.Extensions.Networking;
using Chaos.IO.Memory;
using Chaos.Networking.Entities.Server;
using Chaos.NLog.Logging.Definitions;
using Chaos.NLog.Logging.Extensions;
using Chaos.Packets;
using Chaos.Packets.Abstractions;
using Chaos.Packets.Abstractions.Definitions;
Expand Down Expand Up @@ -199,14 +201,16 @@ void InnerCatch()
var hex = BitConverter.ToString(buffer.ToArray()).Replace("-", " ");
var ascii = Encoding.ASCII.GetString(buffer);

Logger.LogCritical(
ex,
"Exception while handling a packet for {@ClientType}. (Count: {Count}, Offset: {Offset}, BufferHex: {BufferHex}, BufferAscii: {BufferAscii})",
GetType().Name,
Count,
offset,
hex,
ascii);
Logger.WithTopics(Topics.Entities.Client, Topics.Entities.Packet, Topics.Actions.Processing)
.WithProperty(this)
.LogError(
ex,
"Exception while handling a packet for {@ClientType}. (Count: {Count}, Offset: {Offset}, BufferHex: {BufferHex}, BufferAscii: {BufferAscii})",
GetType().Name,
Count,
offset,
hex,
ascii);
}

InnerCatch();
Expand Down Expand Up @@ -256,7 +260,13 @@ public virtual void Send(ref ServerPacket packet)
//no way to pass the packet in because its a ref struct
//but we still want to avoid serializing the packet to a string if we aren't actually going to log it
if (LogRawPackets)
Logger.LogTrace("[Snd] {Packet}", packet.ToString());
Logger.WithTopics(
Topics.Qualifiers.Raw,
Topics.Entities.Client,
Topics.Entities.Packet,
Topics.Actions.Send)
.WithProperty(this)
.LogTrace("[Snd] {Packet}", packet.ToString());

packet.ShouldEncrypt = Crypto.ShouldEncrypt((byte)packet.OpCode);

Expand Down
Loading

0 comments on commit 21c9382

Please sign in to comment.