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

@ -19,6 +19,7 @@ using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Server.Logging;
namespace Server
{
@ -210,12 +211,10 @@ namespace Server
}
else
{
Utility.PushColor(ConsoleColor.Red);
Persistence.WriteConsoleLine($"***** Bad deserialize of {t.GetType()} *****");
Persistence.WriteConsoleLine(error);
Utility.PopColor();
Console.WriteLine($"***** Bad deserialize of {t.GetType()} *****");
Console.WriteLine(error);
Persistence.WriteConsoleLine("Delete the object and continue? (y/n)");
Console.WriteLine("Delete the object and continue? (y/n)");
if (Console.ReadKey(true).Key != ConsoleKey.Y)
{
@ -245,20 +244,20 @@ namespace Server
if (t?.IsAbstract != false)
{
Persistence.WriteConsoleLine("failed");
Console.WriteLine("failed");
var issue = t?.IsAbstract == true ? "marked abstract" : "not found";
Persistence.WriteConsoleLine($"Error: Type '{typeName}' was {issue}. Delete all of those types? (y/n)");
Console.WriteLine($"Error: Type '{typeName}' was {issue}. Delete all of those types? (y/n)");
if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
types.Add(null);
Persistence.WriteConsole("Loading...");
Console.WriteLine("Loading...");
continue;
}
Persistence.WriteConsoleLine("Types will not be deleted. An exception will be thrown.");
Console.WriteLine("Types will not be deleted. An exception will be thrown.");
throw new Exception($"Bad type '{typeName}'");
}

View file

@ -22,6 +22,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Server.Guilds;
using Server.Logging;
using Server.Network;
namespace Server
@ -37,6 +38,8 @@ namespace Server
public static class World
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(World));
private static readonly ManualResetEvent m_DiskWriteHandle = new(true);
private static readonly Dictionary<Serial, IEntity> _pendingAdd = new();
private static readonly Dictionary<Serial, IEntity> _pendingDelete = new();
@ -154,7 +157,7 @@ namespace Server
{
if (WorldState != WorldState.Saving)
{
WriteConsoleLine($"Attempting to queue {item} for decay but the world is not saving");
logger.Warning($"Attempting to queue {item} for decay but the world is not saving");
return;
}
@ -245,7 +248,7 @@ namespace Server
WorldState = WorldState.Loading;
WriteConsole("Loading...");
logger.Information("Loading world");
var watch = Stopwatch.StartNew();
Persistence.Load(_savePath);
@ -273,15 +276,11 @@ namespace Server
watch.Stop();
Utility.PushColor(ConsoleColor.Green);
Console.Write("done");
Utility.PopColor();
Console.WriteLine(
" ({1} items, {2} mobiles) ({0:F2} seconds)",
logger.Information(string.Format("World loaded ({1} items, {2} mobiles) ({0:F2} seconds)",
watch.Elapsed.TotalSeconds,
Items.Count,
Mobiles.Count
);
));
WorldState = WorldState.Running;
}
@ -309,7 +308,7 @@ namespace Server
var message =
$"Warning: Attempted to {action} {entity} during world save.{Environment.NewLine}This action could cause inconsistent state.{Environment.NewLine}It is strongly advised that the offending scripts be corrected.";
WriteConsoleLine(message);
logger.Information(message);
try
{
@ -383,7 +382,7 @@ namespace Server
try
{
var watch = Stopwatch.StartNew();
WriteConsole("Writing snapshot...");
logger.Information("Writing snapshot...");
Persistence.WriteSnapshot(tempPath);
@ -475,7 +474,7 @@ namespace Server
var now = DateTime.UtcNow;
WriteConsole("Saving...");
logger.Information("Saving world");
var watch = Stopwatch.StartNew();
@ -592,9 +591,7 @@ namespace Server
{
if (_pendingDelete.Remove(entity.Serial))
{
Utility.PushColor(ConsoleColor.Red);
WriteConsoleLine($"Deleted then added {entity.GetType().Name} during {WorldState.ToString()} state.");
Utility.PopColor();
logger.Warning($"Deleted then added {entity.GetType().Name} during {WorldState.ToString()} state.");
}
_pendingAdd[entity.Serial] = entity;
break;
@ -656,19 +653,5 @@ namespace Server
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveGuild(BaseGuild guild) => Guilds.Remove(guild.Serial);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteConsole(string message)
{
var now = DateTime.UtcNow;
Console.Write("[{0} {1}] World: {2}", now.ToShortDateString(), now.ToLongTimeString(), message);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteConsoleLine(string message)
{
var now = DateTime.UtcNow;
Console.WriteLine("[{0} {1}] World: {2}", now.ToShortDateString(), now.ToLongTimeString(), message);
}
}
}