Click here to Skip to main content
14,734,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While setting everything for Serilog and SEQ I was stuck in an issue that you may have an answer for it. I am trying to add some properties to all logs using LogContext.PushProperty. However, in my middleware (see image below) the LogContext can push property to logs before await next.Invoke(). While after await next when I try to use LogContext it doesn't push any property to log messages. The issue is that claims are always empty before the await next.Invoke() and they only have values after await next so I am forced to use LogContext after the await but it doesn't work there as mentioned. Please advise if you have a clue?


What I have tried:

// var user2 = context.User as IAMClaimsUser;
// if (user2 != null && !string.IsNullOrEmpty(user2.FirstName))
// {
//     LogContext.PushProperty("UserEmail", user2.Email);
// }

await next.Invoke();

var user2 = context.User as IAMClaimsUser;
if (user2 != null && !string.IsNullOrEmpty(user2.FirstName))
    LogContext.PushProperty("UserEmail2", user2.Email);
Updated 6-Jul-20 2:45am
Richard Deeming 6-Jul-20 7:46am
Something in the middleware chain is replacing the user with an object which does not implement the IAMClaimsUser interface.

You will need to debug your code to find out why. We can't do that for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900