Skip to content
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

Fix possible deadlock issue with AspNetLayoutRendererBase.Register #632

Closed
snakefoot opened this issue Feb 15, 2021 · 11 comments · Fixed by #659
Closed

Fix possible deadlock issue with AspNetLayoutRendererBase.Register #632

snakefoot opened this issue Feb 15, 2021 · 11 comments · Fixed by #659
Assignees
Labels
ASP.NET Core ASP.NET Core - all versions bug
Milestone

Comments

@snakefoot
Copy link
Contributor

snakefoot commented Feb 15, 2021

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.

@snakefoot
Copy link
Contributor Author

Could at the same time suppress ObjectDisposedException when logging outside-request-scope. See also: #630

@304NotModified
Copy link
Member

304NotModified commented Feb 27, 2021

We need to change things in NLog for this.

  • it should be possible to register FuncLayoutRenderer (or a like)
  • FuncLayoutRenderer.RenderMethod method should be settable (protected) and a protected ctor without this method is needed.

Another option is to copy FuncLayoutRenderer, but then you're missing IStringValueRenderer for example and coping code is never really nice (e.g. FormatAsJson)

Also...

  • I don't know why FuncLayoutRenderer.RenderMethod is public
  • I don't know why FuncLayoutRenderer is public, but it's nice for this

@304NotModified 304NotModified added this to the 4.12 milestone Feb 27, 2021
@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 27, 2021 via email

@304NotModified
Copy link
Member

Yes, but we still need the changes in NLog itself ;)

@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 27, 2021 via email

@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 27, 2021

Maybe something like this (if you think FunctionLayoutRenderer is the best thing):

internal class NLogWebLayoutRenderer : FunctionLayoutRenderer
{
     Func<LogEventInfo, HttpContextBase, LoggingConfiguration, object> _func;

     private IHttpContextAccessor HttpContextAccessor { get; set; }   // TODO Missing implementation

     public NLogWebLayoutRenderer(string name, Func<LogEventInfo, HttpContextBase, LoggingConfiguration, object> func)
         :base(name, (event, config) => LookupValue(event, config))
     {
         _func = func;
     }

     private object LookupValue(LogEventInfo logEvent, LoggingConfiguration config)
     {
          var httpContext = HttpContextAccessor?.HttpContext;
          return _func(logEvent, httpContext, config);
     }
}

Alternative one could have FunctionLayoutRenderer as a member an initialize it when needing it, and instead inherit from AspNetLayoutRendererBase (And get HttpContextAccessor for free)

@304NotModified
Copy link
Member

I will look into this :)

@304NotModified
Copy link
Member

304NotModified commented Feb 27, 2021

LookupValue needs to be static then (and so also HttpContextAccessor property). And I don't think we like static "cache"

If static is ok, we could add it to the AspNetLayoutRendererBase. But that doesn't look right

@304NotModified 304NotModified added ASP.NET Core ASP.NET Core - all versions and removed ASP.NET Core 1 labels Feb 27, 2021
@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 27, 2021

Then I guess FunctionLayoutRenderer has to be improved, by adding a new protected virtual method called RenderValue() that returns object (Think messing with RenderMethod-property is a mistake).

And at some point this special implementation of FunctionLayoutRenderer will use Resolve<T> to lookup the HttpContext-accessor.

@304NotModified
Copy link
Member

yes indeed, see NLog/NLog#4326 :)

@304NotModified
Copy link
Member

304NotModified commented Mar 28, 2021

working on this --> #659

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASP.NET Core ASP.NET Core - all versions bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants