Use CrimsonLogs to Add Logging to Your Mod
Updated 2 months agoAdding CrimsonLog as an optional dependency
Step 1) Add CrimsonLog as a SOFT dependency. This ensures that Crimson will be loaded before your mod.
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency("CrimsonLog", BepInDependency.DependencyFlags.SoftDependency)]
internal class Plugin : BasePlugin
Step 2) Create a way to check for CrimsonLog, this can be in your main plugin.cs or some other utility script.
public static bool LogLoaded = false;
public override void Load()
{
Instance = this;
Settings = new Settings();
_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
foreach (var plugin in IL2CPPChainLoader.Instance.Plugins)
{
var metadata = plugin.Value.Metadata;
if (metadata.GUID.Equals("CrimsonLog"))
{
// found it
LogLoaded = true;
break;
}
}
}
Step 3)
Use Logger, as it's an optional dependency make sure your logs will go to bepinex if users don't use it.
public static class Logger
{
public static void Log(string message, bool console = false)
{
if (Plugin.LogLoaded && CrimsonLog.Plugin.LogInstance != null && !console)
{
CrimsonLog.Systems.Logger.Record("ChatMonitor", "chat", message + "\n");
}
else
{
Plugin.LogInstance.LogInfo(message);
}
}
}