Unit Testing

Advanced Unity Debugging with log4net

This tutorial will teach you how to use log4net to write logs to multiple sources in Unity.

Logging is an important part of programming, even in game development. The Apache Software Foundation’s website claims that approximately 4% of all code is dedicated to it, and it’s easy to see why.

Apart from helping you chase down bugs, logs can also serve as a form of documentation. Programs that log their every move are simultaneously creating a written record of how they function. This information is invaluable to understanding what your code is doing at runtime.

But the best logs in the world are utterly useless if you can’t find them. That’s where the ability to write your logs to multiple sources comes in handy.

The Apache log4net Library

There are plenty of tools that can write logs to more than one source. However, this tutorial will focus on log4net.

The Apache log4net library is a logging tool that gives you control over how and where your log statements are written. It’s a popular choice among developers because it’s open source and has great community support.

Out of the box, log4net can write logs to a variety of useful sources, such as rolling logs and databases. But to get it to work in Unity you’ll need to show it how to write to Unity’s built-in Debug class. That’s what this tutorial will cover

Tutorial

Step 1 – Dependencies

Download the latest log4net DLL from https://logging.apache.org/log4net.

Then import the DLL into your Unity project. Make sure you select the DLL that’s compatible with the Scripting Runtime Version of your project.

Step 2 – Custom Appender

Log4net doesn’t know how to write logs to the Unity console out of the box. You’ll need to show it how using a custom Appender.

Copy the code below and add it to your project.

Step 3 – Configure Logging With log4net

Now that you have a custom Appender you’ll need to configure log4net to use it. A common practice among developers is to use an XML file to set everything up, so go ahead and add the following file to your Unity project’s Asset folder.

For more on configuring log4net visit https://logging.apache.org/log4net/release/manual/configuration.html.

Step 4 -Initialize Logging

Log4net must be initialized with the XML config file before it’ll begin writing logs. You have to be sure to do this before any logs are created.

You can do this in Unity using the RuntimeInitializeOnLoadMethod attribute. This attribute triggers the method that it’s marking to be executed either right before or after the scene is loaded.

Copy the code below and add it to your project.

The configure method calls log4net’s built in XmlConfigurator and passes in the path to the config file that you created in step 3.

Step 5 – Start Logging!

Congratulation, you’ve successfully added log4net to your Unity project. Begin logging by creating an instance of ILog using the LogManager. A common practice is to add a private static readonly ILog field to each class.

Passing in the class type into GetLogger provides the log messages context about where they came from.

Downloads

Summary

Logging is an important part of programming, even in game development. One of my biggest game programming tips is to make sure your that all of your projects have some sort of log to file functionality.

With log4net you can write log statements to multiple sources, making it a powerful tool for debugging in Unity.

Newsletter

Subscribe to my email newsletter for Unity programming tutorials and content related to game development.