Locking in middleware #51
-
Is GlobalLogContext.Lock() required in a constructor in an ASP.NET Core middleware? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey @gitzko, As for your second question, It's hard to answer without knowing more details of your app, but fundamentally I'd recommend that you set the property at the closest place where you have the value for it... If the middleware constructor is where you first have the value you want to set globally, then that seems like the right place for it. |
Beta Was this translation helpful? Give feedback.
Hey @gitzko,
Lock
(orLockAsync
) is not needed in a constructor of an ASP .NET Core middleware because the middleware is constructed only once per application lifetime, so there's no multiple threads writing to theGlobalLogContext
.As for your second question, It's hard to answer without knowing more details of your app, but fundamentally I'd recommend that you set the property at the closest place where you have the value for it... If the middleware constructor is where you first have the value you want to set globally, then that seems like the right place for it.