Log

Logging system for debugging and error reporting.

namespace Xengine.Core;
public static class Log

Log Levels

TraceVery detailed debugging
DebugDebug information
InfoGeneral information
WarningPotential issues
ErrorErrors that can be recovered
FatalCritical errors

Methods

// Basic logging
Log.Trace("Very detailed info");
Log.Debug("Debug message");
Log.Info("Player spawned");
Log.Warning("Low health!");
Log.Error("Failed to load resource");
Log.Fatal("Critical system failure");

// With source identifier
Log.Info("Connected to server", "Network");
Log.Error("Packet lost", "Network");

// Logging exceptions
try {
    // risky code
} catch (Exception ex) {
    Log.Error(ex, "Failed to process");
}

Configuration

// Set minimum log level
Log.MinLevel = Log.LogLevel.Debug;

// Toggle output
Log.WriteToConsole = true;
Log.WriteToFile = true;

// Include timestamps and source
Log.IncludeTimestamp = true;
Log.IncludeSource = true;

Example: Custom Logger

public class GameManager : Component
{
    protected override void OnStart()
    {
        Log.Info("Game started!", "GameManager");
    }
    
    public void LoadLevel(string name)
    {
        Log.Debug($"Loading level: {name}", "GameManager");
        
        try {
            // Load level
            Log.Info($"Level '{name}' loaded", "GameManager");
        } catch (Exception ex) {
            Log.Error(ex, $"Failed to load level: {name}");
        }
    }
}

Log Events

// Subscribe to log events
Log.OnLog += (entry) => {
    if (entry.Level >= Log.LogLevel.Warning)
    {
        // Show in-game notification
        ShowNotification(entry.Message);
    }
};

// Get log history
var history = Log.GetHistory();
foreach (var entry in history)
{
    Console.WriteLine($"{entry.Timestamp}: {entry.Message}");
}