Serilog.Enrichers.Unity
April 24, 2025 · View on GitHub

Overview
This package contains a Serilog enricher that dynamically adds Unity data to your log events.
If you find this package useful, consider supporting its development!
Unity® and the Unity logo are trademarks of Unity Technologies.
Installation
This package must be installed from a Git URL.
Simply follow the instructions at that link, using the following URL:
https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/Serilog.Enrichers.Unity#<branch>
Replace <branch> with one of the branch names described in the UnityUtil installing docs (e.g., unity6).
You can ignore the steps about installing Odin Inspector in those docs.
This package has only been tested on Unity 6, but it should work with earlier Unity versions as well.
Usage
Note
Consider installing the following packages as well to get the best developer experience with Serilog in Unity projects:
- Serilog.Sinks.Unity or Serilog.Sinks.Unity3D to add Unity as a Serilog sink
- Unity.Extensions.Logging for some other useful Microsoft.Extensions.Logging extension methods for Unity
- Unity.Extensions.Serilog for some other useful Serilog extension methods for Unity
To use this enricher with default settings, simply add code like the following:
var logger = new Serilog.LoggerConfiguration()
.Enrich.WithUnityData()
// Configure other Serilog enrichers, sinks, etc.
.CreateLogger();
To configure which log properties are added by this enricher, and the names of those properties,
you can provide an explicit UnityLogEnricherSettings object, like so:
var logger = new Serilog.LoggerConfiguration()
.Enrich.WithUnityData(new UnityLogEnricherSettings {
WithUnscaledTime = true,
UnscaledTimeLogProperty = "UT",
})
// Configure other Serilog enrichers, sinks, etc.
.CreateLogger();
This would add a LogEventProperty to your LogEvents with name "UT"
and value equal to Unity's Time.unscaledTime at the time of the log.
You might change the property name, e.g., to avoid collisions with property names added by other enrichers, or if you wanted to save a few bytes with shorter property names in your production logs.
Tip
You can easily add additional, unchanging properties from Unity not covered by this enricher
using Serilog's built-in Enrich.WithProperty() method. For example:
var logger = new Serilog.LoggerConfiguration()
.Enrich.WithProperty("Platform", Application.platform)
// ...
.CreateLogger();
Warning
The above approach must only be used for values that do not change during the application's lifetime.
For example, if you added Unity's Time properties
in this way then every log would show time values from when the logger configuration was defined (typically app start),
not from when an actual log event occurs!
Read the XML docs on UnityLogEnricher for a list of all log properties
that this enricher can add.
Legal
Copyright © 2024 - present Derploid® Entertainment, LLC. All rights reserved.
Unity® and the Unity logo are trademarks of Unity Technologies.
