trenoncourt / serilog-enrichers-aspnetcore-httpcontext

Enriches Serilog events with Aspnetcore HttpContext.
MIT License
38 stars 17 forks source link

added capability for custom context logging #4

Closed dahlsailrunner closed 5 years ago

dahlsailrunner commented 5 years ago

Hi there! I haven't actually EVER done code-based pull requests in the OSS community before, so this is a first for me. Feel free to completely reject if you like.

But here's what I've done, which basically amounts to the capability to either add the user information from httpcontext as a claims identity by specifying a bool = true value when using the .WithAspnetcoreHttpcontext() extension, and also to be able to invoke a totally custom method to create whatever log properties you wanted (cookies, session, pick and choose request items, etc.).

I was trying hard to get it so that the IHttpContextAccessor was available BEFORE the webhostbuilder was created, but couldn't figure it out.

I got the UserInfo stuff tested and working (with the custom method and the simple bool) against the demo identity server at https://demo.identityserver.io and can post the code in by own GitHub repo if you want to see it. But it's pretty sweet (and simple).

If you don't want this type of thing in the repo or you want me to adjust what I've done in any just let me know.

Thanks - Erik

dahlsailrunner commented 5 years ago

Just in case you wanted to see my other demo code, it's here: https://github.com/dahlsailrunner/enrichers-demo/tree/master/SimpleUI

trenoncourt commented 5 years ago

hi @dahlsailrunner,

Good stuff, thanks for your contribution :)

trenoncourt commented 5 years ago

@martinmani,

Sorry for my poor availability during last two weeks. I just updated the nuget package to 1.0.1.

dahlsailrunner commented 5 years ago

I'm thinking I might do another PR to clean this up just a bit and add some documentation. Any objections?

jepperc commented 5 years ago

@dahlsailrunner I'm not using this as a package but based some code on the userinfo thing.. I got some issues since claims are not guaranteed to be a dictionary, as there can be more values per key. So if you're changing things anyway, I would recommend doing a List<KeyValuePair<string,string>> instead :)

dahlsailrunner commented 5 years ago

Yep @jepperc that's one of the things I wanted to clean up. Thanks for the note.

dahlsailrunner commented 5 years ago

Should including cookies be an option? Like bool parameter like I did for user info? For my own purposes I don't want them in the logs. Or maybe a better option would be that if the custom action is specified that we don't use the HttpContextCache at all and the user / developer can put whatever they want in the entry?