Logging

IntermediateProgrammer

You can log information about your game while it runs using Log.

Unlike profiling, which retrieves information automatically, it's up to you to create your own log messages and define when they're triggered. For example, you can create a log message that triggers when a character performs a certain action. This is useful to investigate how your game is performing.

Note

Logging is disabled when you build the game in release mode.

When you use logging and run your game in debug mode, Xenko opens a console in a second window to display logging information. The messages are color-coded by level. The name of the module (such as the script containing the log message) is displayed in brackets. This is followed by the log level (eg Warning, Error, etc), then the log message.

Logging in console

The console displays log messages from all modules, not just your own scripts. For example, it also displays messages from the ContentManager.

If you run your game from Visual Studio, log messages are shown in the Visual Studio Output window instead.

Log output window

Log levels

There are six levels of log message, used for different levels of severity.

Log levelColorDescription
DebugGrayStep-by-step information for advanced debugging purposes
VerboseWhiteDetailed information
InfoGreenGeneral information
WarningYellowMinor errors that might cause problems
ErrorRedErrors
FatalRedSerious errors that crash the game

By default, the log displays messages for the level Info and higher. This means it doesn't display Debug or Verbose messages. To change this, see Set the minimum level below.

Write a log message

In the script containing code you want to log, write:

  1. Log.Debug("My log message");

You can replace Debug with the level you want to use for the log message (see Log levels above).

You can combine this with if statements to log this message under certain conditions (see Example script below).

Set the log level

You can set a minimum log level to display. For example, if you only want to see messages as severe as Warning or higher, use:

  1. Log.ActivateLog(LogMessageType.Warning);
Note

This isn't a global setting. The log level you set only applies to the script you set it in.

Change the log level at runtime

  1. ((Game)Game).ConsoleLogLevel = LogMessageType.myLogLevel;

Disable a specific log

  1. GlobalLogger.GetLogger("RouterClient").ActivateLog(LogMessageType.Debug, LogMessageType.Fatal, false);
  2. // Disables logging of the RouterClient module

Disable logging in the console

  1. ((Game)Game).ConsoleLogMode = ConsoleLogMode.None;

Create a log file

To save the log output to a text file, add this code to the Start method:

  1. var fileWriter = new TextWriterLogListener(new FileStream("myLogFile.txt", FileMode.Create));
  2. GlobalLogger.GlobalMessageLogged += fileWriter;

This creates a file in the Debug folder of your project (eg MyGame\MyGame\Bin\Windows\Debug\myLogFile.txt).

Example script

The following script checks that the texture MyTexture is loaded. When the texture loads, the log displays a debug message (Log.Error). If it doesn't load, the log records an error message (Log.Debug).

  1. using System.Linq;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Xenko.Core.Diagnostics;
  5. using Xenko.Core.Mathematics;
  6. using Xenko.Input;
  7. using Xenko.Engine;
  8. using Xenko.Graphics;
  9. namespace MyGame
  10. {
  11. public class Script : SyncScript
  12. {
  13. public Texture myTexture;
  14. public override void Start()
  15. {
  16. // Initialization of the script.
  17. Log.ActivateLog(LogMessageType.Debug);
  18. Log.Debug("Start loading MyTexture");
  19. myTexture = Content.Load<Texture>("MyTexture");
  20. if (myTexture == null)
  21. {
  22. Log.Error("MyTexture not loaded");
  23. }
  24. else
  25. {
  26. Log.Debug("MyTexture loaded successfully");
  27. }
  28. }
  29. }
  30. }

See also