RetrieveHttpContextAccessor is a dangerous method, and should not be called frequently, because it can introduce deadlocks. See also: NLog/NLog#2064
Instead one should create a new class FuncAspNetLayoutRenderer, that can hold the func-delegate and perform caching of HttpContextAccessor.