Table of Contents

Logging

BSGameLib logs through Microsoft.Extensions.Logging, and has no logging provider of its own. The host's defaults add a console provider, which shows Information and above. The Logging section of appsettings.json changes the levels, as it does in any host.

Categories

Each logger has a category, which the levels and filters match against:

  • ILogger, injected without a type argument, has the category BSGameLib.Server.
  • ILogger<T> has the full name of T as its category, so a class's own messages can be filtered apart from the others.
using BSGameLib.Networking;
using BSGameLib.Server;
using Microsoft.Extensions.Logging;

public class PingPacket : IDataPacket
{
  public long SentAt { get; set; }
}

public class PingHandler(ILogger<PingHandler> logger) : IDataPacketHandler<PingPacket>
{
  public void Handle(INetConnection connection, PingPacket packet) =>
    logger.LogDebug("Ping from {Address}", connection.IpAddress);
}

The engine logs failing packet handlers and failing game ticks as errors, and client connections at Information. Its detailed messages are at Debug and Trace.

Levels in appsettings.json

The Logging section sets a default level and overrides for each category. This keeps the game's own messages at Debug and quiets the server's:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "MyGame": "Debug",
      "BSGameLib.Server": "Warning"
    }
  }
}

Changing the logging

ConfigureHost in a subclass of ServerHost changes the host builder, and logging is set there. This example raises the minimum level for every category:

using BSGameLib.Server;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

public class MyServerHost(IServerSettings settings) : ServerHost(settings)
{
  protected override void ConfigureHost(IHostBuilder builder) =>
    builder.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Trace));
}

To send logs to a file or another system, use that system's package from ConfigureHost. For example, Serilog is set up with UseSerilog on the host builder, from the Serilog.Extensions.Hosting package, which your game references; BSGameLib doesn't reference it. Put the Serilog setup in ConfigureHost the same way, and configure its sinks there.