Enjoy The Loop Blog
Sign in

Instrument the code

One of my games in the Microsoft Store started logging a lot of exceptions. The problem was that the Dispose() method, which was causing the exceptions, did a lot of different things, so I needed to figure out exactly where the problem was.

When I asked Chippy what could be causing the issue, he said:

"At this point I'd instrument Dispose()."

What does instrument mean here? I had to look it up.

In debugging, instrument means temporarily adding logging or diagnostics to your code so you can see exactly what it's doing at runtime.

For example, during development:

public override void Dispose() {
    Debug.WriteLine("Dispose: 1");
    _resizeThrottler.RedrawNeeded -= OnRedrawNeeded;

    Debug.WriteLine("Dispose: 2");
    _resizeThrottler.Dispose();

    Debug.WriteLine("Dispose: Finished");
}

If the output is:

Dispose: 1
Dispose: 2

then you know the code must have crashed at:

_resizeThrottler.Dispose();

Another technique is to wrap each section in a try/catch block so you can log exactly where the problem occurred:

try {
    _resizeThrottler.RedrawNeeded -= OnRedrawNeeded;
}
catch (Exception ex) {
    LogException("Failed unsubscribing RedrawNeeded: " + ex);
    throw;
}

This tells you exactly which statement threw the exception. Once you've identified and fixed the problem, you can remove the instrumentation from the code.

Written by Loek van den Ouweland on July 24, 2026.