AlanBarber / log4net.Appender.Splunk

A log4net appender for Splunk Http Event Collector (HEC) Sender
Apache License 2.0
7 stars 16 forks source link

Code exits function before posting log. #20

Closed andrewdci03 closed 2 years ago

andrewdci03 commented 2 years ago

We have .NET Console app that we're developling that when every it hits the response = await postEvents(token, events); it exits out of the function with out ever getting to responseCode = response.StatusCode;. This all occurs in the HttpEvenCollectorSender.cs file, I'll post the whole function in question below:

private async Task<HttpStatusCode> PostEvents(
            List<HttpEventCollectorEventInfo> events,
            String serializedEvents)
        {
            // encode data
            HttpResponseMessage response = null;
            string serverReply = null;
            HttpStatusCode responseCode = HttpStatusCode.OK;
            try
            {
                // post data
                HttpEventCollectorHandler next = (t, e) =>
                {
                    HttpContent content = new StringContent(serializedEvents, Encoding.UTF8, HttpContentTypeMedia);
                    return httpClient.PostAsync(httpEventCollectorEndpointUri, content);
                };
                HttpEventCollectorHandler postEvents = (t, e) =>
                {
                    return middleware == null ?
                        next(t, e) : middleware(t, e, next);
                };
                response = await postEvents(token, events);
                responseCode = response.StatusCode;
                if (responseCode != HttpStatusCode.OK && response.Content != null)
                {
                    // record server reply
                    serverReply = await response.Content.ReadAsStringAsync();
                    OnError(new HttpEventCollectorException(
                        code: responseCode,
                        webException: null,
                        reply: serverReply,
                        response: response,
                        events: events
                    ));
                }
            }
            catch (HttpEventCollectorException e)
            {
                e.Events = events;
                OnError(e);
            }
            catch (Exception e)
            {                
                OnError(new HttpEventCollectorException(
                    code: responseCode,
                    webException: e,
                    reply: serverReply,
                    response: response,
                    events: events
                ));
            }
            return responseCode;
        }

I assume it's having a hard time running the function HttpEventCollectorHandler postEvents = (t, e) => { return middleware == null ? next(t, e) : middleware(t, e, next); };

I will say this function pattern I'm not familiar with, and I'm not an expert in asynchronous programming yet either.

andrewdci03 commented 2 years ago

This can be closed as we realized that the console app was closing before the async methods that were sending the log could finish.