12
12
using System . IO ;
13
13
using System . Linq ;
14
14
using Microsoft . Extensions . Hosting ;
15
+ using Microsoft . Extensions . Configuration ;
15
16
16
17
namespace CommonSchema
17
18
{
@@ -20,6 +21,14 @@ namespace Server
20
21
public class Startup
21
22
{
22
23
private int seq = 0 ;
24
+ private static object locker = new Object ( ) ;
25
+
26
+ public Startup ( IConfiguration configuration )
27
+ {
28
+ Configuration = configuration ;
29
+ }
30
+
31
+ public IConfiguration Configuration { get ; }
23
32
24
33
// This method gets called by the runtime. Use this method to add services to the container.
25
34
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
@@ -54,24 +63,41 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
54
63
try
55
64
{
56
65
string path = context . Request . Path . Value ;
57
- if ( path . StartsWith ( "/OneCollector/ " ) )
66
+ if ( path . StartsWith ( "/OneCollectorWriteToFile " ) )
58
67
{
59
- int length = Int32 . Parse ( context . Request . Headers [ "Content-Length" ] ) ;
60
- BinaryReader reader = new BinaryReader ( context . Request . Body ) ;
61
-
62
- // Read body fully before decoding it
63
- byte [ ] buffer = reader . ReadBytes ( length ) ;
64
-
65
- Dictionary < string , string > headers = new Dictionary < string , string > ( ) ;
66
- foreach ( KeyValuePair < string , StringValues > entry in context . Request . Headers )
68
+ string result = DecodeTelemetryRequest ( context , decoderLogger ) ;
69
+ var fileName = Configuration . GetSection ( "FileNameToStoreTelemetryData" ) ? . Value ;
70
+ if ( ! string . IsNullOrEmpty ( fileName ) )
67
71
{
68
- // Our decoder only needs to know the 1st header value, do not need a multimap
69
- headers [ entry . Key ] = entry . Value . ElementAt ( 0 ) ;
70
- } ;
71
- Decoder decoder = new Decoder ( headers , buffer ) ;
72
- // Supply the logger
73
- decoder . Logger = decoderLogger ;
74
- string result = decoder . ToJson ( false , true , 2 ) ;
72
+ lock ( locker )
73
+ {
74
+ if ( File . Exists ( fileName ) )
75
+ {
76
+ var formattedResult = result . Replace ( "[" , "" ) . Replace ( "]" , "" ) ;
77
+ var currentContent = File . ReadAllText ( fileName ) ;
78
+ var updatedContent = string . Concat ( currentContent . Replace ( "]" , "," ) , formattedResult , "]" ) ;
79
+ File . WriteAllText ( fileName , updatedContent ) ;
80
+ }
81
+ else
82
+ {
83
+ File . AppendAllText ( fileName , result ) ;
84
+ }
85
+ }
86
+ }
87
+ else
88
+ {
89
+ requestLogger . LogError ( "Parameter FileNameToStoreTelemetryData from appsettings.json, where data should be stored is not specified. As a result telemetry data won't be saved to file, but will be visible in the console" ) ;
90
+ }
91
+
92
+ // Echo the body converted to JSON array
93
+ context . Response . StatusCode = 200 ;
94
+ requestLogger . LogInformation ( result ) ;
95
+ await context . Response . WriteAsync ( result ) ;
96
+ }
97
+ else
98
+ if ( path . StartsWith ( "/OneCollector/" ) )
99
+ {
100
+ string result = DecodeTelemetryRequest ( context , decoderLogger ) ;
75
101
76
102
// Echo the body converted to JSON array
77
103
context . Response . StatusCode = 200 ;
@@ -126,6 +152,27 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
126
152
} ) ;
127
153
128
154
}
155
+
156
+ private static string DecodeTelemetryRequest ( HttpContext context , ILogger decoderLogger )
157
+ {
158
+ int length = Int32 . Parse ( context . Request . Headers [ "Content-Length" ] ) ;
159
+ BinaryReader reader = new BinaryReader ( context . Request . Body ) ;
160
+
161
+ // Read body fully before decoding it
162
+ byte [ ] buffer = reader . ReadBytes ( length ) ;
163
+
164
+ Dictionary < string , string > headers = new Dictionary < string , string > ( ) ;
165
+ foreach ( KeyValuePair < string , StringValues > entry in context . Request . Headers )
166
+ {
167
+ // Our decoder only needs to know the 1st header value, do not need a multimap
168
+ headers [ entry . Key ] = entry . Value . ElementAt ( 0 ) ;
169
+ } ;
170
+ Decoder decoder = new Decoder ( headers , buffer ) ;
171
+ // Supply the logger
172
+ decoder . Logger = decoderLogger ;
173
+ string result = decoder . ToJson ( false , true , 2 ) ;
174
+ return result ;
175
+ }
129
176
}
130
177
}
131
178
}
0 commit comments