1
- using HttpReports . Core ;
1
+ using HttpReports . Core ;
2
2
using HttpReports . Core . Models ;
3
3
using Microsoft . Extensions . Logging ;
4
- using Microsoft . Extensions . Options ;
4
+ using Microsoft . Extensions . Options ;
5
5
using System ;
6
6
using System . Collections . Generic ;
7
7
using System . Net ;
8
- using System . Net . Http ;
8
+ using System . Net . Http ;
9
9
using System . Text . Json ;
10
10
using System . Threading ;
11
11
using System . Threading . Tasks ;
@@ -15,14 +15,13 @@ namespace HttpReports.Transport.Http
15
15
{
16
16
public class HttpTransport : IReportsTransport
17
17
{
18
- public HttpTransportOptions _options { get ; }
18
+ public HttpTransportOptions _options { get ; }
19
19
20
- private readonly AsyncCallbackDeferFlushCollection < RequestBag > _RequestBagCollection ;
20
+ private readonly AsyncCallbackDeferFlushCollection < RequestBag > _RequestBagCollection ;
21
21
22
22
private readonly ILogger < HttpTransport > _logger ;
23
23
private readonly IHttpClientFactory _httpClientFactory ;
24
24
private readonly JsonSerializerOptions _jsonSetting ;
25
- private readonly JsonSerializerOptions jsonSerializer ;
26
25
27
26
28
27
public HttpTransport ( IOptions < HttpTransportOptions > options , JsonSerializerOptions jsonSetting , ILogger < HttpTransport > logger , IHttpClientFactory httpClientFactory )
@@ -31,31 +30,35 @@ public HttpTransport(IOptions<HttpTransportOptions> options, JsonSerializerOptio
31
30
_logger = logger ;
32
31
_httpClientFactory = httpClientFactory ;
33
32
_jsonSetting = jsonSetting ;
34
- _RequestBagCollection = new AsyncCallbackDeferFlushCollection < RequestBag > ( Push , _options . DeferThreshold , _options . DeferSecond ) ;
35
- }
33
+ if ( _options . CollectorAddress != null )
34
+ _RequestBagCollection = new AsyncCallbackDeferFlushCollection < RequestBag > ( Push , _options . DeferThreshold , _options . DeferSecond ) ;
35
+ }
36
36
37
37
public Task SendDataAsync ( RequestBag bag )
38
- {
39
- _RequestBagCollection . Flush ( bag ) ;
38
+ {
39
+ if ( _RequestBagCollection != null )
40
+ _RequestBagCollection . Flush ( bag ) ;
40
41
41
42
return Task . CompletedTask ;
42
- }
43
+ }
43
44
44
45
public async Task SendDataAsync ( Performance performance )
45
46
{
46
- await Retry ( async ( ) => {
47
+ if ( _options . CollectorAddress == null ) return ;
48
+ await Retry ( async ( ) =>
49
+ {
47
50
48
51
try
49
52
{
50
- HttpContent content = new StringContent ( HttpUtility . HtmlEncode ( System . Text . Json . JsonSerializer . Serialize ( performance , _jsonSetting ) ) , System . Text . Encoding . UTF8 , "application/json" ) ;
53
+ HttpContent content = new StringContent ( HttpUtility . HtmlEncode ( System . Text . Json . JsonSerializer . Serialize ( performance , _jsonSetting ) ) , System . Text . Encoding . UTF8 , "application/json" ) ;
51
54
52
55
content . Headers . ContentType = new System . Net . Http . Headers . MediaTypeHeaderValue ( "application/json" ) ;
53
56
content . Headers . Add ( BasicConfig . TransportType , typeof ( Performance ) . Name ) ;
54
57
55
58
var response = await _httpClientFactory . CreateClient ( BasicConfig . HttpReportsHttpClient ) . PostAsync ( _options . CollectorAddress + BasicConfig . TransportPath . Substring ( 1 ) , content ) ;
56
59
57
60
return response . StatusCode == HttpStatusCode . OK ;
58
- }
61
+ }
59
62
catch ( Exception ex ) when ( ex is HttpRequestException || ex is TaskCanceledException )
60
63
{
61
64
_logger . LogWarning ( "HttpReports transport failed..." ) ;
@@ -65,57 +68,59 @@ await Retry(async () => {
65
68
{
66
69
//_logger.LogError(ex, "performance push failed:" + ex.ToString());
67
70
return false ;
68
- }
71
+ }
69
72
70
73
} ) ;
71
74
72
- }
75
+ }
73
76
74
77
private async Task Push ( List < RequestBag > list , CancellationToken token )
75
- {
76
- await Retry ( async ( ) => {
78
+ {
79
+ if ( _options . CollectorAddress == null ) return ;
80
+ await Retry ( async ( ) =>
81
+ {
77
82
78
- try
79
- {
80
- HttpContent content = new StringContent ( HttpUtility . HtmlEncode ( System . Text . Json . JsonSerializer . Serialize ( list , _jsonSetting ) ) , System . Text . Encoding . UTF8 ) ;
83
+ try
84
+ {
85
+ HttpContent content = new StringContent ( HttpUtility . HtmlEncode ( System . Text . Json . JsonSerializer . Serialize ( list , _jsonSetting ) ) , System . Text . Encoding . UTF8 ) ;
81
86
82
- content . Headers . ContentType = new System . Net . Http . Headers . MediaTypeHeaderValue ( "application/json" ) ;
83
- content . Headers . Add ( BasicConfig . TransportType , typeof ( RequestBag ) . Name ) ;
84
- var response = await _httpClientFactory . CreateClient ( BasicConfig . HttpReportsHttpClient ) . PostAsync ( _options . CollectorAddress + BasicConfig . TransportPath . Substring ( 1 ) , content ) ;
87
+ content . Headers . ContentType = new System . Net . Http . Headers . MediaTypeHeaderValue ( "application/json" ) ;
88
+ content . Headers . Add ( BasicConfig . TransportType , typeof ( RequestBag ) . Name ) ;
89
+ var response = await _httpClientFactory . CreateClient ( BasicConfig . HttpReportsHttpClient ) . PostAsync ( _options . CollectorAddress + BasicConfig . TransportPath . Substring ( 1 ) , content ) ;
85
90
86
- var result = await response . Content . ReadAsStringAsync ( ) ;
91
+ var result = await response . Content . ReadAsStringAsync ( ) ;
87
92
88
- return response . StatusCode == HttpStatusCode . OK ;
93
+ return response . StatusCode == HttpStatusCode . OK ;
89
94
90
- }
91
- catch ( Exception ex ) when ( ex is HttpRequestException || ex is TaskCanceledException )
92
- {
93
- _logger . LogWarning ( "HttpReports transport failed..." ) ;
94
- return false ;
95
- }
96
- catch ( Exception ex )
97
- {
98
- _logger . LogWarning ( "HttpReports transport failed:" + ex . ToString ( ) ) ;
99
- return false ;
100
- }
95
+ }
96
+ catch ( Exception ex ) when ( ex is HttpRequestException || ex is TaskCanceledException )
97
+ {
98
+ _logger . LogWarning ( "HttpReports transport failed..." ) ;
99
+ return false ;
100
+ }
101
+ catch ( Exception ex )
102
+ {
103
+ _logger . LogWarning ( "HttpReports transport failed:" + ex . ToString ( ) ) ;
104
+ return false ;
105
+ }
101
106
102
- } ) ;
103
- }
107
+ } ) ;
108
+ }
104
109
105
110
private async Task < bool > Retry ( Func < Task < bool > > func , int retry = 3 )
106
- {
111
+ {
107
112
for ( int i = 0 ; i < 3 ; i ++ )
108
113
{
109
114
await Task . Delay ( 200 ) ;
110
115
111
116
if ( await func . Invoke ( ) )
112
117
{
113
118
return true ;
114
- }
119
+ }
115
120
}
116
121
117
122
return false ;
118
- }
123
+ }
119
124
120
125
}
121
126
}
0 commit comments