@@ -26,9 +26,17 @@ public class HttpClientEventDispatcher45 : IEventDispatcher
2626 public ILogger Logger { get ; set ; } = new DefaultLogger ( ) ;
2727
2828 /// <summary>
29- /// Timeout for the HTTP request (10 seconds)
29+ /// HTTP client object.
3030 /// </summary>
31- const int TIMEOUT_MS = 10000 ;
31+ private static readonly HttpClient Client ;
32+
33+ /// <summary>
34+ /// Constructor for initializing static members.
35+ /// </summary>
36+ static HttpClientEventDispatcher45 ( )
37+ {
38+ Client = new HttpClient ( ) ;
39+ }
3240
3341 /// <summary>
3442 /// Dispatch an Event asynchronously
@@ -38,26 +46,20 @@ private async void DispatchEventAsync(LogEvent logEvent)
3846 try
3947 {
4048 string json = logEvent . GetParamsAsJson ( ) ;
41-
42- using ( var client = new HttpClient ( ) )
49+ var request = new HttpRequestMessage
4350 {
44- client . Timeout = TimeSpan . FromMilliseconds ( TIMEOUT_MS ) ;
45-
46- var request = new HttpRequestMessage
47- {
48- RequestUri = new Uri ( logEvent . Url ) ,
49- Method = HttpMethod . Post ,
50- // The Content-Type header applies to the Content, not the Request itself
51- Content = new StringContent ( json , System . Text . Encoding . UTF8 , "application/json" ) ,
52- } ;
51+ RequestUri = new Uri ( logEvent . Url ) ,
52+ Method = HttpMethod . Post ,
53+ // The Content-Type header applies to the Content, not the Request itself
54+ Content = new StringContent ( json , System . Text . Encoding . UTF8 , "application/json" ) ,
55+ } ;
5356
54- foreach ( var h in logEvent . Headers )
55- if ( h . Key . ToLower ( ) != "content-type" )
56- request . Content . Headers . Add ( h . Key , h . Value ) ;
57+ foreach ( var h in logEvent . Headers )
58+ if ( h . Key . ToLower ( ) != "content-type" )
59+ request . Content . Headers . Add ( h . Key , h . Value ) ;
5760
58- var result = await client . SendAsync ( request ) ;
59- result . EnsureSuccessStatusCode ( ) ;
60- }
61+ var result = await Client . SendAsync ( request ) ;
62+ result . EnsureSuccessStatusCode ( ) ;
6163 }
6264 catch ( Exception ex )
6365 {
0 commit comments