Skip to main content

Logging

Logging in a MoBro plugin is straightforward. You can inject a Microsoft.Extensions.Logging.ILogger into the plugin's constructor. For more details, refer to In depth: Using services.

Plugin.cs
using Microsoft.Extensions.Logging;

public class Plugin : IMoBroPlugin
{
private readonly ILogger _logger;

public Plugin(ILogger logger)
{
_logger = logger;
}

public void Init()
{
_logger.LogInformation("Initializing plugin");
}
}

The logger instance provided to the plugin is already configured and fully integrated into the MoBro data service's logging system.


Local Testing

When testing a plugin locally as a console application (as described in In depth: Testing - Start locally), the injected logger instance logs to the console by default.
The default log level during local testing is set to DEBUG.

To use a custom logger, you can provide it via the MoBroPluginBuilder when creating the plugin, as shown below:

Program.cs
// Create custom logger
var logger = ...
// Create and start the plugin locally with a custom logger
var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithSetting("update_frequency", "1")
.WithLogger(logger) // Use custom logger
.Build();

// Prevent the program from exiting immediately
Console.ReadLine();

Alternatively, you can change the default log level using MoBroPluginBuilder without needing to pass a custom logger:

var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithLogLevel(LogEventLevel.Warning) // Set log level to Warning
.Build();

When Installed to MoBro

Once a plugin is installed in MoBro, the default log level is set to Information.

Log outputs are handled as follows:

  1. Forwarded to the MoBro data service: Included in the service log for debugging purposes.
  2. Written to a plugin-specific log file: Stored in the plugin’s installation directory.
  • Log files rotate daily or when the file size reaches 10MB.
  • The last 14 log files are retained.

Additionally, whenever a debug.zip is generated in MoBro, it includes the complete log history of all plugins.

Accessing Log Files

The current log file of an installed plugin is accessible via the REST API of the MoBro data service: localhost:42069/api/logs/plugins/[plugin_name]
Replace the [plugin_name] with the unique plugin name as configured in the mobro_plugin_config.json