-
Notifications
You must be signed in to change notification settings - Fork 809
Made LogEvent reusable #1772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made LogEvent reusable #1772
Conversation
I just realized that LogEvent is a public class so I can't really change its API as I thought. |
You may use https://www.nuget.org/packages/Microsoft.Extensions.ObjectPool/ out of the |
|
Unfortunately it also seems that LogEvent instances might be collected by enrichers and sinks (like in some unit tests) which makes it impossible to automatically determine when they are free to be returned to the pool. My next best idea is to make them readonly structs that are passed around as |
I've been some distance down this path in a "Serilog v3" - in a nutshell, changing Got this working, but there's a lot of complexity and churn for a comparatively modest improvement in throughput. Concluded that it's probably not worthwhile at this point, though it's something I have regularly come back to. |
This does not mean that caching is impossible. It is possible, you just need to provide this feature as opt-in.
|
Enrichers are sync in most cases. Sinks may be async, yes. Look into var logEvent = new LogEvent(logTimestamp, level, exception, parsedTemplate, boundProperties);
Dispatch(logEvent); Good candidate for caching especially for those who do not use asynchronous sinks. Just acquire log event from the pool before @nblumhardt I can post a draft PR to evaluate. |
I think that is what I tried making in this branch of mine: https://github.com/igor84/serilog/tree/reusablelogevent. If you check that out and try it out you will find a number of unit tests failing because they are collecting LogEvents and checking them at the end where they turn out wrong because they were reused. |
My implementation works since caching is opt-in feature. I will post demo PR some time later. |
No description provided.