Add support for Serilog.Extensions.Logging #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Based on the discussion in #9, here's support for using
UnityEngine.Object
s withBeginScope()
and using the latest pushed scope as context when sending following log entries to Unity's console window.This way, log entries are clickable and e.g.
GameObject
s get pinged in the editor.On key design choice from a user's perspective was to avoid additonal APIs or dependencies. User code only needs to reference
Microsoft.Extensions.Logging.ILogger
and work with the abstractions. Everything else is done upfront via configuration.Example
Config:
Usage:
Async/await support
It also works with async/await code as long as it is managed by Unity's main thread. UniTask should work fine, too.
No thread safety for UnityEngine.Objects
This stems from the known Unity limitation: Access to all/most members of its managed-lifecycle objects is limited to the main thread.
So when calling e.g.
BeginScope(gameObject)
on other threads the library throws anNotSupportedException
to keep it from failing somewhere else down the line (like in enrichers or sinks).Everything else works fine from other threads.
Optional integration
The new functionality lives in a separate assembly (
Serilog.Sinks.Unity3D.Extensions.Logging
). The assembly definition makes sure it only compiles when the dependencies are in place, namely the packageorg.nuget.serilog.extensions.logging
(which in turn depends on Serilog and Microsoft's abstractions).If the dependencies are not found, the base sink (Serilog.Sinks.Unity3D) still works just like before.
Installation via manifest.json