Contextual Logging in C#
Logging is vital for debugging and understanding your game. ConsolePro, a Unity asset that I highly recommend, includes advanced filtering options such as being able to filter based on regex. It exposes a weakness of Unity's logging however: there is no context baked into the log itself! If you want to only find logs from a specific class, you have to make sure to add that literally to the string every time, which can be a pain. Or, if you want to filter based on domain (audio, gameplay, and UI for example), you have to manually type those in too! No good. Let's create a wrapper around Unity's logging system, so that we can include these much needed elements:
Here, we're adding [Classname] to the start of every log. We could change this to instead be a domain, a namespace, or whatever we want! Its surprising how useful this simple change can be to tracking down hard to hit bugs, especially when you're doing adhoc debug lines and want to filter by your class. This becomes especially important for larger projects, as there can be thousands of logs per second at the debug level. Hope this is useful, and go log your important events!