serilog-web / classic

[Discontinued] Serilog web request logging and enrichment for classic ASP.NET applications
Apache License 2.0
79 stars 32 forks source link

WithUserName Only Working With HttpModule Logging But Doesnt Log During Web Service/API Logs #75

Closed Raj4git closed 3 years ago

Raj4git commented 3 years ago

I have configured logging with below values .Enrich.WithUserName(noneUsername: "not found") .Enrich.WithClaimValue(ClaimTypes.NameIdentifier, "TestUserName");

Its getting logged correctly when code logs HTTP calls but for any service/web api calls from the app it doesn't get log

httpcall service log
tsimbalar commented 3 years ago

Hi,

this enrichers really only adds as a log property the contents of HttpContext.Current.User. Can you confirm that HttpContext.Current.User is actually set for your web api calls ?

xantari commented 3 years ago

There are also two username fields in that screenshot, looks like you might be confusing "UserName" and "Username" (notice the slight case sensitivity difference).

Raj4git commented 3 years ago

"Username"(derived using HttpContext.Current.User) is manually pushed in the logs before it write. "UserName" gets added by web classic enricher. I suspect its a cross-thread issue. :(

tsimbalar commented 3 years ago

Not quite sure I understand what you mean when you say :

for any service/web api calls from the app it doesn't get log

Could you paste some snippets of the code that writes down those logs ?

Raj4git commented 3 years ago

When we land on our home page we call a web API. We are trying to log the username when we call API along with api request-response and other details. I can see serilog configured to log user name correctly showing username for httpmodule logs but not showing username for the web api logs. Sorry cant add code.

tsimbalar commented 3 years ago

@Raj4git thanks for the bug report.

To be honest, I really don't know what could be causing it, and therefore how to fix it :-/

To retrieve the user, we actually do something like :

(new HttpContextWrapper(HttpContext.Current)).User

does this return something different in your case of a service call ?

xantari commented 3 years ago

My guess is @Raj4git has not actually inspected the .User property that @tsimbalar is referencing.

@Raj4git is indicating this is a web api. My bet is he is authenticating via Identity Server. It doesn't wire itself up by default to hydrate the Username property upon receiving JWT tokens... You have to do some manual magic to make that happen. So in essence @Raj4git username property is null because of incorrect configuration of his Web API configuration.

tsimbalar commented 3 years ago

Closing for lack of activity. Happy to re-open if needed