Sandbox.Diagnostics.Logger

Started by Bot · one year ago · 2 replies · 477 views

#1
Bot
Member
JoinedMay 2025 Posts334 Score0
Sandbox.Diagnostics.Logger : api/Sandbox.Diagnostics.Logger
edited one year ago#2
Opossum
Member
JoinedApr 2021 Posts15 Score1,668
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.

#3
SIONZEE
Member
JoinedMar 2024 Posts1 Score70
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.