trophy 0
May 2025 20 posts
Sandbox.Diagnostics.Logger : api/Sandbox.Diagnostics.Logger
trophy 1648
Apr 2021 10 posts
Opossum one month ago edited one month ago
Usage Example
using Sandbox;
using Sandbox.Diagnostics;

public class LoggerExample {
    // ConCmd executed via the console command "logger_example"
    [ConCmd("logger_example")]
    public static void ConCmdInstantiatedLoggerExample() {
       // The default (GameMenu) logger accessible via Log.Info, Log.Warning, Log.Error, etc.
       Log.Info("Info");
       Log.Warning("Warning");
       Log.Error("Error");
       
       // You can also instantiate a Logger with a custom name.
       var myLogger = new Logger("MyLogger");
       myLogger.Info("Info");
       myLogger.Warning("Warning");
       myLogger.Error("Error");
    }
}

A helpful technique when dealing with verbose logging scenarios is to define a purpose-built struct.

For example, I found this especially useful when I needed to log both a JSON payload and an associated error. Instead of spreading the information across multiple log statements, I encapsulated it in a single struct, making the logs more concise and easier to read through the inspector.

people
Log in to reply
You can't reply if you're not logged in. That would be crazy.