Logging to the mojoPortal log4net log From Custom Code

mojoPortal CMS uses log4net for logging errors and other system information. You can view the log under Administration > System Log, or you can download the log file which is located at /Data/currentlog.config. It is also possible to log to the database.

To log errors or debug information or other information from custom feature code, you first need to add a reference from your project to the log4net.dll file in the _libs folder.

Then you can declare a logger and log exceptions as shown in the following code snippet:

using log4net;

namespace MyProject.Web
	public partial class MyCustomControl : SiteModuleControl
		private static readonly ILog log = LogManager.GetLogger(typeof(MyCustomControl));

		protected void Page_Load(object sender, EventArgs e)
				//try to do something here
			catch (Exception ex) //try to be more specific with the exception type
				log.Error("failed to do something: ", ex);

Note that you can use log.Info("your message"); to log information other than exceptions to the log.

For debugging purposes you can use log.Debug("your debug message");

Whether debug logging appears in the log depends on the LOG LEVEL configured in the log4net.config file in the root of the site, the default level in mojoPortal is INFO which is a higher log level than error so INFO messages will be shown by default.

<level value="INFO" />

The log4net levels are shown above in their priority. So for example with the level set to INFO, it will not log debug messages, if the level is set to ERROR it will not log INFO messages, If the level is set to OFF, it will not log anything at all. 

So in code you can use log.Debug() to log details that you only need to see when debugging, and when you want to see those messages you set the level to DEBUG. This creates much more verbose logging, so when not debugging its best to turn the level back down to INFO, but this allows you to leave the debugging log statements in your code without creating a lot of noise during normal INFO logging.

Last Modified by Shawn Grout on Jul 11, 2022