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

Override Default logging #141

Closed
nishantkagrawal opened this Issue Jul 5, 2018 · 8 comments

Comments

Projects
None yet
5 participants
@nishantkagrawal

nishantkagrawal commented Jul 5, 2018

Is there a way to override the default logging or disable it?

@rynowak

This comment has been minimized.

Member

rynowak commented Jul 5, 2018

Can you tell us a little more about what you want to do?

@nishantkagrawal

This comment has been minimized.

nishantkagrawal commented Jul 6, 2018

When I use HttpClientFactory this is what the log record message looks like

Start processing HTTP request POST https://my.domain.com/accounts

I want it to have more information such as

{"LogMessage":"Sending request: [POST https://my.domain.com/accounts] [operation: 233c2bf5-83b7-4f5e-b12c-2e9a02cc2e36]","Request":{"Method":"POST","Uri":"https://my.domain.com/accounts","Headers":{"Accept":"application/json","apikey":"testingKey","Content-Type":"application/json; charset=utf-8"}}}

So basically I need a way to control what is logged, and actually at what level. I have added a custom HttpMessageHandler that does this for me, but then I see duplicate logs.

@martincostello

This comment has been minimized.

Contributor

martincostello commented Jul 6, 2018

More detailed logs are available in-box at Trace level, but then you may encounter performance issues with the sheer amount of data produced at runtime, plus also potentially logging sensitive information such as authorisation headers.

If you need to customise the logs, a custom message handler is the way to go. You should just then filter out messages from the in-box logging in your configuration if you don’t want duplicates: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#log-filtering

@rynowak

This comment has been minimized.

Member

rynowak commented Jul 7, 2018

If you want to take control and implement your own thing, I'd suggest removing the default implementation of IHttpMessageHandlerBuilderFilter from services and writing your own.

If you follow all of the links here, you'll see how our default logging implementation is stitched together.
https://github.com/aspnet/HttpClientFactory/search?q=IHttpMessageHandlerBuilderFilter&unscoped_q=IHttpMessageHandlerBuilderFilter

I'm also interested if you have ideas about how to make the logging better or more configurable.

@stevejgordon

This comment has been minimized.

Contributor

stevejgordon commented Jul 8, 2018

@rynowak I've been meaning to blog about the logging included in IHttpClientFactory for a while. This piqued my interest again so I'll aim to blog a simple example of what you suggest here too.

@stevejgordon

This comment has been minimized.

Contributor

stevejgordon commented Jul 9, 2018

I've completed and published my post about the logging in HttpClientFactory + added an additional piece around @rynowak's suggestion above to swap out the filter - https://www.stevejgordon.co.uk/httpclientfactory-asp-net-core-logging

@nishantkagrawal

This comment has been minimized.

nishantkagrawal commented Jul 9, 2018

@rynowak : While logging one of the thing that we always want to log is the correlation id. Also for all outgoing http requests via httpclient, we log an extra operation id for all the logs for the specific HttpClient, that helps us to filter the logs to a specific HttpOperation. That's the primary reason why I would like to override the default logger within HttpClient.

@stevejgordon : I looked at your blog post and it looks awesome. In my case, I just created another MessageHandler instead of overriding the logs created by default, and filtered the default logs within the log4net framework.

@glennc

This comment has been minimized.

Member

glennc commented Aug 3, 2018

Looks like we've answered the question, so I'm going to close this. I also think we should put an issue on the backlog about building something that lets you configure what you want to log and log it, it's a feature we've talked about but haven't really started designing or working on.

@glennc glennc closed this Aug 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment