Serilog.Sinks.Unity3D
Serilog.Sinks.Unity3D copied to clipboard
Serilog Sink for Unity 3D Debug log
Serilog.Sinks.Unity3D
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 Serilog[^SerilogVersion] 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
context[^EnrichUnityContext] 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
[^EnrichUnityContext]: This is done with a new extension method override which explicitly accepts UnityEngine.Object
.
[^SerilogVersion]: Version 2.12.0 or later