Serilog.Sinks.Unity3D
December 6, 2022 · View on GitHub
Serilog sink for Unity3D, logs to Unity Debugger
Usage
Installation
Install it through OpenUPM or use the UnityPackage from the Releases page.
openupm add com.serilog.sinks.unity3d
Dependencies
You need add Serilog1 to your project. Your usual options:
- Xoofx's UnityNuGet server (preferred)
- Manual add the Serilog DLL to your assets folder
- NuGetForUnity
Creating the logger
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Unity3D()
.CreateLogger();
If you have a custom implementation of Unity's ILogger interface, then you can log to that:
ILogger myCustomLogger = new MyCustomLogger();
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Unity3D(unityLogger: myCustomLogger)
.CreateLogger();
If no logger is provided the library will use UnityEngine.Debug.unityLogger (which is equivalent of using UnityEngine.Debug.Log() methods)
Unity log extras
You can provide the UnityEngine.Object context2 and tag parameters for the logger:
public class MyObject : MonoBehaviour
{
// ...
private ILogger _logger = new();
public void DoLog()
{
_logger
.ForContext(this)
.WithUnityTag("My custom tag")
.Information("This is an info log");
}
}
Migration guide
For versions before 2.0.0
Set up MainThreadDispatcher.Unity
Upgrade from 1.0.0 to 1.0.1
You need to provide the following DLLs:
- Serilog
- MainThreadDispatcher