Skip to main content

Logging

To write log messages, inject a Microsoft.Extensions.Logging.ILogger into the plugin's constructor. For more details, refer to Using Services.

Plugin.cs
using Microsoft.Extensions.Logging;
using MoBro.Plugin.SDK;

namespace Plugin.Example;

public class Plugin : IMoBroPlugin
{
private readonly ILogger _logger;

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

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

The injected logger is already configured and integrated into MoBro's logging.


Local Testing​

When running a plugin locally, the injected logger writes to the console.
The default log level is Debug.

The log level of the default logger can be changed using MoBroPluginBuilder.WithLogLevel. The level is a Serilog LogEventLevel, since the SDK uses Serilog internally:

Program.cs
using MoBro.Plugin.SDK;
using Serilog.Events;

using var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithLogLevel(LogEventLevel.Warning)
.Build();

Console.ReadLine();

Alternatively, any custom Microsoft.Extensions.Logging.ILogger can be passed using MoBroPluginBuilder.WithLogger:

Program.cs
using Microsoft.Extensions.Logging;
using MoBro.Plugin.SDK;
using Serilog;

// Create a custom logger (here: Serilog writing to the console with a custom output format)
using var loggerFactory = LoggerFactory.Create(builder => builder.AddSerilog(
new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console(outputTemplate: "[{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger()
));

using var plugin = MoBroPluginBuilder
.Create<Plugin.Example.Plugin>()
.WithLogger(loggerFactory.CreateLogger("Plugin"))
.Build();

Console.ReadLine();

When Installed to MoBro​

Once a plugin is installed in MoBro, the log level is Information. Debug messages are not written.

Log messages are written to:

  1. The MoBro service log, for debugging purposes.
  2. A plugin-specific log file, located in the log folder inside the plugin's installation directory.
    • Log files rotate daily or when the file size reaches 10 MB.
    • The last 14 log files are retained.

Additionally, the debug .zip export generated in MoBro includes the log files of all plugins.

Accessing Log Files​

The current log file of an installed plugin is available via the local REST API of MoBro:

curl.exe http://localhost:42069/api/logs/plugins/example_plugin

Replace example_plugin with the name of the plugin as configured in its mobro_plugin_config.json.