@@ -26,9 +26,17 @@ public class HttpClientEventDispatcher45 : IEventDispatcher
26
26
public ILogger Logger { get ; set ; } = new DefaultLogger ( ) ;
27
27
28
28
/// <summary>
29
- /// Timeout for the HTTP request (10 seconds)
29
+ /// HTTP client object.
30
30
/// </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
+ }
32
40
33
41
/// <summary>
34
42
/// Dispatch an Event asynchronously
@@ -38,26 +46,20 @@ private async void DispatchEventAsync(LogEvent logEvent)
38
46
try
39
47
{
40
48
string json = logEvent . GetParamsAsJson ( ) ;
41
-
42
- using ( var client = new HttpClient ( ) )
49
+ var request = new HttpRequestMessage
43
50
{
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
+ } ;
53
56
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 ) ;
57
60
58
- var result = await client . SendAsync ( request ) ;
59
- result . EnsureSuccessStatusCode ( ) ;
60
- }
61
+ var result = await Client . SendAsync ( request ) ;
62
+ result . EnsureSuccessStatusCode ( ) ;
61
63
}
62
64
catch ( Exception ex )
63
65
{
0 commit comments