Table of Contents

Getting Started with Diagnostics from End User Machines

This guide will explain how to implement the LogSystemInformation method in a MonoGame project, which logs various system and platform information to a text file using the StreamWriter class. The code also shields the user's name in the current directory and assembly location by replacing it with “REDACTED_USER”.

The code is written in C# and requires the following using statements at the top of the file:

Usings

using System;
using System.IO;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Framework.Utilities;

The code assumes that the class inherits from the Game class, which is the base class for MonoGame projects. The code also defines some private fields for the graphics device manager, the sprite batch, the current directory, and the user name.

Fields

public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private string currentDirectory;
private string userName;

The constructor sets some basic properties for the game, such as the content root directory and the mouse visibility.

public Game1()
{
// This code remains untouched in this tutorial
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}

The Initialize method overrides the base method and calls the LogSystemInformation method before calling the base.Initialize method.

Initialize()

protected override void Initialize()
{
// Call the method to log system information
LogSystemInformation();
base.Initialize();
}

The LogSystemInformation method is the main focus of this guide. It creates a text file named “SystemInfoDebug.txt” and writes various system and platform information to it using the StreamWriter class. The second parameter of the StreamWriter constructor is set to false to overwrite the file if it already exists.

LogSystemInformation()

private void LogSystemInformation()
{
// Using the StreamWriter class to write to a text file
// The second StreamWriter parameter is set to false to overwrite the file
using (StreamWriter writer = new StreamWriter(filePath, false))
{
// resumed after the next text block

The method uses the PlatformInfo class from the MonoGame.Framework.Utilities namespace to access the graphics backend and the MonoGame platform, and writes them to the file.

// Accessing the graphics backend
var graphicsBackend = PlatformInfo.GraphicsBackend;
writer.WriteLine("Graphics Backend: " + graphicsBackend);
// Accessing the MonoGame platform
var monoGamePlatform = PlatformInfo.MonoGamePlatform;
writer.WriteLine("MonoGame Platform: " + monoGamePlatform);
// resumed after the next text block

The method also uses the Environment class from the System namespace to access various system information, such as the operating system, the processor count, the 64 bit status, the system directory, the CLR version, and the system page size, and writes them to the file.

// Additional system information
writer.WriteLine("Operating System: " + Environment.OSVersion);
writer.WriteLine("Processor Count: " + Environment.ProcessorCount);
writer.WriteLine("64 Bit Operating System: " + Environment.Is64BitOperatingSystem);
// Shielding user's name in the current directory
currentDirectory = Environment.CurrentDirectory;
userName = Environment.UserName;
if (currentDirectory.Contains(userName))
{
currentDirectory = currentDirectory.Replace(userName, "REDACTED_USER");
}
writer.WriteLine("Current Directory: " + currentDirectory);
writer.WriteLine("System Directory: " + Environment.SystemDirectory);
writer.WriteLine("CLR Version: " + Environment.Version);
writer.WriteLine("System Page Size: " + Environment.SystemPageSize);
// resumed after the next text block

The method also uses the Assembly class from the System.Reflection namespace to access the executing assembly and its location, and writes them to the file. It also shields the user's name in the assembly location by replacing it with “REDACTED_USER”.

// Assembly information
Assembly executingAssembly = Assembly.GetExecutingAssembly();
writer.WriteLine("Executing Assembly: " + executingAssembly.FullName);
string assemblyLocation = executingAssembly.Location;
// Shielding user's name in the assembly location
{
assemblyLocation = assemblyLocation.Replace(userName, "REDACTED_USER");
}
writer.WriteLine("Location: " + assemblyLocation);
// resumed after the next text block

Finally, the method optionally prints to the debug window that the information has been logged, using the System.Diagnostics.Debug.WriteLine method.

// Optionally, print to the debug window that the information has been logged
System.Diagnostics.Debug.WriteLine("System information logged to " + filePath);
}
}

The rest of the code is the standard MonoGame template code for loading content, updating logic, and drawing graphics.

Remainder Code Untouched

protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
// Included here for clarity

This concludes the guide on how to implement the LogSystemInformation method in a MonoGame project. The code can be tested by running the game and checking the SystemInfoDebug.txt file in the bin folder of the project.

Example Output

A possible sample output of the text file is:

Graphics Backend: DirectX
MonoGame Platform: Windows
Operating System: Microsoft Windows NT 6.2.9200.0
Processor Count: 4
64 Bit Operating System: True
64 Bit Process: True
Current Directory: C:\Users\REDACTED_USER\Documents\Visual Studio 2019\Projects\MonoGameSample\bin\Windows\x86\Debug
System Directory: C:\Windows\system32
CLR Version: 4.0.30319.42000
System Page Size: 4096
Executing Assembly: MonoGameSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Location: C:\Users\REDACTED_USER\Documents\Visual Studio 2019\Projects\MonoGameSample\bin\Windows\x86\Debug\MonoGameSample.exe