bruno-garcia / log4net.ElasticSearch

log4net appender to ElasticSearch
https://bruno-garcia.github.io/log4net.ElasticSearch/
Other
219 stars 92 forks source link

Httpclient - httpresponse issue #121

Closed koderi-dp closed 4 years ago

koderi-dp commented 4 years ago

Hi,

when using this library in net core console app 2.2, i get exception

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.HttpWebResponse'.

i think problem is in this part (httpResponse.Close)

              var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
              httpResponse.Close();
              if (httpResponse.StatusCode != HttpStatusCode.Created && httpResponse.StatusCode != HttpStatusCode.OK)

why not change it to

  using (var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse())
                {
                    if (httpResponse.StatusCode != HttpStatusCode.Created && httpResponse.StatusCode != HttpStatusCode.OK)
                    {
                        throw new WebException("Failed to post {0} to {1}.".With(postBody.ToString(), uri));
                    }
                }

then it is working

bruno-garcia commented 4 years ago

@koderi-dp do you have a stack trace so we can point to the exact line number? You suggest by then it is working because you've made that change and verified the fix?

You could add Sentry to your app so errors would be sent there even if your logging isn't working.

koderi-dp commented 4 years ago

@koderi-dp do you have a stack trace so we can point to the exact line number? You suggest by then it is working because you've made that change and verified the fix?

You could add Sentry to your app so errors would be sent there even if your logging isn't working.

Hi, exception was in line where status code is checked, if (httpResponse.StatusCode != HttpStatusCode.Created && httpResponse.StatusCode != HttpStatusCode.OK)

I had to download the source change it like in sugestion (put it in using without manualy closing the connection) and its working without issues, it looks if you acces status code prop of disposed response it will throw exception, didnt go into reflection of source code, maybe connected to net standard or net core version

Library in which your nuget was is in netstandard 2.0 and app that used it was in netcore 2.2

bruno-garcia commented 4 years ago

Makes sense. Would you open a PR with the fix for this? Happy to accept PRs

koderi-dp commented 4 years ago

Makes sense. Would you open a PR with the fix for this? Happy to accept PRs

Sure

koderi-dp commented 4 years ago

122 created