1
- using Logzio . DotNet . NLog ;
2
- using Microsoft . ApplicationInsights . NLogTarget ;
1
+ using Formitable . BetterStack . Logger . Microsoft ;
3
2
using Microsoft . Extensions . DependencyInjection ;
4
3
using Microsoft . Extensions . Hosting ;
5
4
using Microsoft . Extensions . Logging ;
6
- using NLog ;
7
- using NLog . Config ;
8
- using NLog . Extensions . Logging ;
9
- using NLog . Layouts ;
10
- using NLog . Targets ;
5
+ using Microsoft . Extensions . Logging . Console ;
11
6
using System ;
12
7
using System . Linq ;
13
- using System . Text ;
14
8
using System . Threading ;
15
9
using System . Threading . Tasks ;
16
10
@@ -22,9 +16,6 @@ public static async Task Main(string[] args)
22
16
{
23
17
var serializerContext = GenerateAttributesForSerializerContext ( ) ;
24
18
25
- var builder = Host . CreateApplicationBuilder ( ) ;
26
- builder . Services . AddHostedService < TestsRunner > ( ) ;
27
-
28
19
// Configure cancellation (this allows you to press CTRL+C or CTRL+Break to stop the integration tests)
29
20
var cts = new CancellationTokenSource ( ) ;
30
21
Console . CancelKeyPress += ( s , e ) =>
@@ -33,16 +24,40 @@ public static async Task Main(string[] args)
33
24
cts . Cancel ( ) ;
34
25
} ;
35
26
36
- // Configure logging
37
- builder . Logging . ClearProviders ( ) ; // Remove the built-in providers (which include the Console)
38
- builder . Logging . AddNLog ( GetNLogConfiguration ( ) ) ; // Add our desired custom providers (which include the Colored Console)
27
+ var services = new ServiceCollection ( ) ;
28
+ ConfigureServices ( services ) ;
29
+ using var serviceProvider = services . BuildServiceProvider ( ) ;
30
+ var app = serviceProvider . GetService < IHostedService > ( ) ;
31
+ await app . StartAsync ( cts . Token ) . ConfigureAwait ( false ) ;
32
+ }
39
33
40
- // Run the tests
41
- var host = builder . Build ( ) ;
42
- await host . StartAsync ( cts . Token ) . ConfigureAwait ( false ) ;
34
+ private static void ConfigureServices ( ServiceCollection services )
35
+ {
36
+ services . AddHostedService < TestsRunner > ( ) ;
43
37
44
- // Stop NLog (which has the desirable side-effect of flushing any pending logs)
45
- LogManager . Shutdown ( ) ;
38
+ services
39
+ . AddLogging ( logging =>
40
+ {
41
+ var betterStackToken = Environment . GetEnvironmentVariable ( "BETTERSTACK_TOKEN" ) ;
42
+ if ( ! string . IsNullOrEmpty ( betterStackToken ) )
43
+ {
44
+ logging . AddBetterStackLogger ( options =>
45
+ {
46
+ options . SourceToken = betterStackToken ;
47
+ options . Context [ "source" ] = "ZoomNet_integration_tests" ;
48
+ options . Context [ "StrongGrid-Version" ] = StrongGrid . Client . Version ;
49
+ } ) ;
50
+ }
51
+
52
+ logging . AddSimpleConsole ( options =>
53
+ {
54
+ options . SingleLine = true ;
55
+ options . TimestampFormat = "yyyy-MM-dd HH:mm:ss " ;
56
+ } ) ;
57
+
58
+ logging . AddFilter ( logLevel => logLevel >= LogLevel . Debug ) ;
59
+ logging . AddFilter < ConsoleLoggerProvider > ( logLevel => logLevel >= LogLevel . Information ) ;
60
+ } ) ;
46
61
}
47
62
48
63
private static string GenerateAttributesForSerializerContext ( )
@@ -85,104 +100,5 @@ private static string GenerateAttributesForSerializerContext()
85
100
var result = string . Join ( "\r \n \r \n " , [ simpleAttributes , arrayAttributes , nullableAttributes ] ) ;
86
101
return result ;
87
102
}
88
-
89
- private static LoggingConfiguration GetNLogConfiguration ( )
90
- {
91
- // Configure logging
92
- var nLogConfig = new LoggingConfiguration ( ) ;
93
-
94
- // Send logs to logz.io
95
- var logzioToken = Environment . GetEnvironmentVariable ( "LOGZIO_TOKEN" ) ;
96
- if ( ! string . IsNullOrEmpty ( logzioToken ) )
97
- {
98
- var logzioTarget = new LogzioTarget
99
- {
100
- Name = "Logzio" ,
101
- Token = logzioToken ,
102
- LogzioType = "nlog" ,
103
- JsonKeysCamelCase = true ,
104
- // ProxyAddress = "http://localhost:8888",
105
- } ;
106
- logzioTarget . ContextProperties . Add ( new NLog . Targets . TargetPropertyWithContext ( "Source" , "StrongGrid_integration_tests" ) ) ;
107
- logzioTarget . ContextProperties . Add ( new NLog . Targets . TargetPropertyWithContext ( "StrongGrid-Version" , StrongGrid . Client . Version ) ) ;
108
-
109
- nLogConfig . AddTarget ( "Logzio" , logzioTarget ) ;
110
- nLogConfig . AddRule ( NLog . LogLevel . Info , NLog . LogLevel . Fatal , logzioTarget , "*" ) ;
111
- }
112
-
113
- // Send logs to Azure Insights
114
- var instrumentationKey = Environment . GetEnvironmentVariable ( "APPLICATIONINSIGHTS_INSTRUMENTATION_KEY" ) ;
115
- if ( ! string . IsNullOrEmpty ( instrumentationKey ) )
116
- {
117
- var applicationInsightsTarget = new ApplicationInsightsTarget ( ) { InstrumentationKey = instrumentationKey , Name = "StrongGrid" } ;
118
- applicationInsightsTarget . ContextProperties . Add ( new Microsoft . ApplicationInsights . NLogTarget . TargetPropertyWithContext ( "Source" , "StrongGrid_integration_tests" ) ) ;
119
- applicationInsightsTarget . ContextProperties . Add ( new Microsoft . ApplicationInsights . NLogTarget . TargetPropertyWithContext ( "StrongGrid-Version" , StrongGrid . Client . Version ) ) ;
120
-
121
- nLogConfig . AddTarget ( "ApplicationInsights" , applicationInsightsTarget ) ;
122
- nLogConfig . AddRule ( NLog . LogLevel . Info , NLog . LogLevel . Fatal , applicationInsightsTarget , "*" ) ;
123
- }
124
-
125
- // Send logs to DataDog
126
- var datadogKey = Environment . GetEnvironmentVariable ( "DATADOG_APIKEY" ) ;
127
- if ( ! string . IsNullOrEmpty ( datadogKey ) )
128
- {
129
- var datadogTarget = new WebServiceTarget ( "datadog" )
130
- {
131
- Url = "https://http-intake.logs.us5.datadoghq.com/v1/input" ,
132
- Encoding = Encoding . UTF8 ,
133
- Protocol = WebServiceProtocol . JsonPost ,
134
- PreAuthenticate = false
135
- } ;
136
-
137
- // DD_API_KEY
138
- // Your Datadog API Key for sending your logs to Datadog.
139
- datadogTarget . Headers . Add ( new MethodCallParameter ( "DD-API-KEY" , Layout . FromString ( datadogKey ) ) ) ;
140
-
141
- // DD_SITE
142
- // The name of your Datadog site.Choose from one of the following examples:
143
- // Example: datadoghq.com(US1), datadoghq.eu(EU), us3.datadoghq.com(US3), us5.datadoghq.com(US5), ddog - gov.com(US1 - FED)
144
- // Default: datadoghq.com(US1)
145
- datadogTarget . Headers . Add ( new MethodCallParameter ( "DD_SITE" , Layout . FromString ( "us5.datadoghq.com(US5)" ) ) ) ;
146
-
147
- // DD_LOGS_DIRECT_SUBMISSION_INTEGRATIONS
148
- // Enables Agentless logging.Enable for your logging framework by setting to Serilog, NLog, Log4Net, or ILogger(for Microsoft.Extensions.Logging).
149
- // If you are using multiple logging frameworks, use a semicolon separated list of variables.
150
- // Example: Serilog; Log4Net; NLog
151
- datadogTarget . Headers . Add ( new MethodCallParameter ( "DD_LOGS_DIRECT_SUBMISSION_INTEGRATIONS" , Layout . FromString ( "NLog" ) ) ) ;
152
-
153
- // DD_LOGS_DIRECT_SUBMISSION_SOURCE
154
- // Sets the parsing rule for submitted logs.
155
- // Should always be set to csharp, unless you have a custom pipeline.
156
- // Default: csharp
157
- datadogTarget . Headers . Add ( new MethodCallParameter ( "DD_LOGS_DIRECT_SUBMISSION_SOURCE" , Layout . FromString ( "csharp" ) ) ) ;
158
-
159
- // DD_LOGS_DIRECT_SUBMISSION_MAX_BATCH_SIZE
160
- // Sets the maximum number of logs to send at one time.
161
- // Takes into account the limits in place for the API.
162
- // Default: 1000
163
-
164
- // DD_LOGS_DIRECT_SUBMISSION_MAX_QUEUE_SIZE
165
- // Sets the maximum number of logs to hold in the internal queue at any one time before dropping log messages.
166
- // Default: 100000
167
-
168
- // DD_LOGS_DIRECT_SUBMISSION_BATCH_PERIOD_SECONDS
169
- // Sets the time to wait(in seconds) before checking for new logs to send.
170
- // Default: 1
171
-
172
- datadogTarget . Headers . Add ( new MethodCallParameter ( "Content-Type" , Layout . FromString ( "application/json" ) ) ) ;
173
- datadogTarget . Headers . Add ( new MethodCallParameter ( "Source" , "StrongGrid_integration_tests" ) ) ;
174
- datadogTarget . Headers . Add ( new MethodCallParameter ( "StrongGrid-Version" , StrongGrid . Client . Version ) ) ;
175
-
176
- nLogConfig . AddTarget ( "DataDog" , datadogTarget ) ;
177
- nLogConfig . AddRule ( NLog . LogLevel . Info , NLog . LogLevel . Fatal , datadogTarget , "*" ) ;
178
- }
179
-
180
- // Send logs to console
181
- var consoleTarget = new ColoredConsoleTarget ( ) ;
182
- nLogConfig . AddTarget ( "ColoredConsole" , consoleTarget ) ;
183
- nLogConfig . AddRule ( NLog . LogLevel . Info , NLog . LogLevel . Fatal , consoleTarget , "*" ) ;
184
-
185
- return nLogConfig ;
186
- }
187
103
}
188
104
}
0 commit comments