seq-client-log4net icon indicating copy to clipboard operation
seq-client-log4net copied to clipboard

Logging mechanism on postasync failure

Open jmatheti opened this issue 7 years ago • 2 comments

Currently the logging mechanism is synchronous and causing system delays in case of seq mis configuration.

The below code (commented), needs to be rewritten by using handling the task gracefully. (Happy to do a pull request if it works )

FileName - Client\Log4Net\SeqAppender.cs Method - SendBuffer

var task = _httpClient.PostAsync(BulkUploadResource, content);
           task.ContinueWith(t =>
           {
               if (!t.Result.IsSuccessStatusCode)
                   ErrorHandler.Error(string.Format("Received failed result {0}: {1}", t.Result.StatusCode, t.Result.Content.ReadAsStringAsync().Result));
           }, TaskContinuationOptions.OnlyOnRanToCompletion);
           task.ContinueWith(t =>
           {               
                   ErrorHandler.Error(string.Format("Received failed result {0}: {1}",t.Exception.Message, t.Exception.StackTrace));
           }, TaskContinuationOptions.OnlyOnFaulted);
           
           //using (var result = _httpClient.PostAsync(BulkUploadResource, content))
           //{
           //    if (!result.IsSuccessStatusCode)
           //        ErrorHandler.Error(string.Format("Received failed result {0}: {1}", result.StatusCode, result.Content.ReadAsStringAsync().Result));
           //}

jmatheti avatar Dec 11 '18 00:12 jmatheti

Thanks for this! There are a couple of considerations if we're going to do this asynchronously:

  • The log event needs its volatile data to be "fixed" so that it's safe and correct for a background thread to access while the logging thread continues
  • If there are a lot of batches to send, they need to be sent sequentially so we don't flood the Seq server with a whole lot of concurrent requests before the earlier requests complete
  • When shutting down, we need to make sure we wait for any background logging to complete

This is a bit challenging with log4net; it may be faster (and more reliable) to try something like: https://github.com/cjbhaines/Log4Net.Async - does this look like an option for you?

nblumhardt avatar Dec 12 '18 04:12 nblumhardt

We've had reports of success using Log4Net.Async; I'll close this but if you still need any help please let us know.

nblumhardt avatar Apr 23 '19 05:04 nblumhardt