<commit_before>using System;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Progstr.Log.Internal
{
    public class LogRequest
    {
        public LogRequest(HttpWebRequest webRequest)
        {
            this.Request = webRequest;    
        }
        
        public HttpWebRequest Request { get; private set; }
        public byte[] Data { get; set; }
        
        public void Start()
        {
            this.Request.ContentLength = this.Data.Length;
            
            this.Request.BeginGetRequestStream(OnGetRequestStream, null);
        }
        
        private void OnGetRequestStream(IAsyncResult result)
        {
            var requestStream = this.Request.EndGetRequestStream(result);
            
            requestStream.BeginWrite(this.Data, 0, this.Data.Length, OnDataWritten, requestStream);
        }
        
        private void OnDataWritten(IAsyncResult result)
        {
            var requestStream = (Stream) result.AsyncState;
            requestStream.EndWrite(result);
            requestStream.Flush();
            requestStream.Close();
            
            this.Request.BeginGetResponse(OnGetResponse, null);
        }
        
        private void OnGetResponse(IAsyncResult result)
        {
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse) this.Request.EndGetResponse(result);
            }
            catch (WebException requestException)
            {
                response = (HttpWebResponse) requestException.Response;
            }
            
            var statusCode = response.StatusCode;
            
            var reader = new StreamReader(response.GetResponseStream());
            var responseBody = reader.ReadToEnd();
            
            if (statusCode != HttpStatusCode.OK)
            {
                Debug.WriteLine("Log HTTP request failed with status: " + statusCode);
                Trace.TraceError("Log HTTP request failed with status: " + statusCode);
                Debug.WriteLine("Response body:\r\n" + responseBody);
                Trace.TraceError("Response body:\r\n" + responseBody);
            }
        }
    }
}
<commit_msg>Better handling for connection errors.<commit_after>using System;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Progstr.Log.Internal
{
    public class LogRequest
    {
        public LogRequest(HttpWebRequest webRequest)
        {
            this.Request = webRequest;    
        }
        
        public HttpWebRequest Request { get; private set; }
        public byte[] Data { get; set; }
        
        public void Start()
        {
            this.Request.ContentLength = this.Data.Length;
            
            this.Request.BeginGetRequestStream(OnGetRequestStream, null);
        }
        
        private void OnGetRequestStream(IAsyncResult result)
        {
            try
            {
                var requestStream = this.Request.EndGetRequestStream(result);
                requestStream.BeginWrite(this.Data, 0, this.Data.Length, OnDataWritten, requestStream);
            }
            catch (WebException connectError)
            { 
                this.OnError(connectError.Status.ToString(), connectError.ToString());
            }
        }
        
        private void OnDataWritten(IAsyncResult result)
        {
            var requestStream = (Stream) result.AsyncState;
            requestStream.EndWrite(result);
            requestStream.Flush();
            requestStream.Close();
            
            this.Request.BeginGetResponse(OnGetResponse, null);
        }
        
        private void OnGetResponse(IAsyncResult result)
        {
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse) this.Request.EndGetResponse(result);
            }
            catch (WebException requestException)
            {
                response = (HttpWebResponse) requestException.Response;
            }
            
            var statusCode = response.StatusCode;
            
            var reader = new StreamReader(response.GetResponseStream());
            var responseBody = reader.ReadToEnd();
            
            if (statusCode != HttpStatusCode.OK)
            {
                this.OnError(statusCode.ToString(), responseBody);
            }
        }

        private void OnError(string status, string body)
        {
            Debug.WriteLine("Log HTTP request failed with status: " + status);
            Trace.TraceError("Log HTTP request failed with status: " + status);
            Debug.WriteLine("Response body:\r\n" + body);
            Trace.TraceError("Response body:\r\n" + body);
        }
    }
}

{'fraction_non_alphanumeric': 0.08175182481751825, 'fraction_numerical': 0.0004171011470281543, 'mean_word_length': 1.6366135239142385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1538689', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1135, 'n_words': 240}