trophy 0
May 2025 34 posts
Sandbox.Diagnostics.Logger : api/Sandbox.Diagnostics.Logger
trophy 1668
Apr 2021 15 posts
Opossum 3 months ago edited 3 months 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.

trophy 70
Mar 2024 1 post
If you wonder how to display LogLevel.Trace, you have to turn on "Trace Logging" in Help menu.

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