@@ -13,8 +13,14 @@ namespace Splunk.Providers
1313 /// </summary>
1414 public abstract class SplunkHECBaseProvider : ILoggerProvider
1515 {
16+ protected ILogger loggerInstance ;
1617 protected HttpClient httpClient ;
1718
19+ public SplunkHECBaseProvider ( SplunkLoggerConfiguration configuration , string endPointCustomization )
20+ {
21+ SetupHttpClient ( configuration , endPointCustomization ) ;
22+ }
23+
1824 /// <summary>
1925 /// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
2026 /// </summary>
@@ -33,14 +39,7 @@ public abstract class SplunkHECBaseProvider : ILoggerProvider
3339 /// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
3440 public abstract void Dispose ( ) ;
3541
36- /// <summary>
37- /// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
38- /// </summary>
39- /// <returns>The logger instance.</returns>
40- /// <param name="categoryName">Category name.</param>
41- public abstract ILogger CreateLoggerInstance ( string categoryName ) ;
42-
43- protected void SetupHttpClient ( SplunkLoggerConfiguration configuration , string endPointCustomization )
42+ void SetupHttpClient ( SplunkLoggerConfiguration configuration , string endPointCustomization )
4443 {
4544 httpClient = new HttpClient
4645 {
@@ -55,69 +54,69 @@ protected void SetupHttpClient(SplunkLoggerConfiguration configuration, string e
5554 httpClient . DefaultRequestHeaders . Add ( "x-splunk-request-channel" , Guid . NewGuid ( ) . ToString ( ) ) ;
5655 }
5756
58- Uri GetSplunkCollectorUrl ( SplunkLoggerConfiguration configuration , string endPointCustomization )
59- {
60- var splunkCollectorUrl = configuration . HecConfiguration . SplunkCollectorUrl ;
61- if ( ! splunkCollectorUrl . EndsWith ( "/" , StringComparison . InvariantCulture ) )
62- splunkCollectorUrl = splunkCollectorUrl + "/" ;
63-
64- if ( ! string . IsNullOrWhiteSpace ( endPointCustomization ) )
65- splunkCollectorUrl = splunkCollectorUrl + endPointCustomization ;
66-
67- if ( configuration . HecConfiguration . ChannelIdType == HECConfiguration . ChannelIdOption . QueryString )
68- splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid . NewGuid ( ) . ToString ( ) ;
69-
70- if ( configuration . HecConfiguration . UseAuthTokenAsQueryString )
71- {
72- var tokenParameter = "token=" + configuration . HecConfiguration . Token ;
73- splunkCollectorUrl = string . Format ( "{0}{1}{2}" , splunkCollectorUrl , splunkCollectorUrl . Contains ( "?" ) ? "&" : "?" , tokenParameter ) ;
74- }
75-
76- return new Uri ( splunkCollectorUrl ) ;
77- }
78-
7957 protected void DebugSplunkResponse ( Task < HttpResponseMessage > responseMessageTask , string loggerType )
8058 {
8159 if ( responseMessageTask . IsCompletedSuccessfully )
82- {
60+ {
8361 switch ( responseMessageTask . Result . StatusCode )
8462 {
8563 case System . Net . HttpStatusCode . OK :
86- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Request completed successfully.") ;
64+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Request completed successfully.") ;
8765 break ;
8866 case System . Net . HttpStatusCode . Created :
89- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Create request completed successfully.") ;
67+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Create request completed successfully.") ;
9068 break ;
9169 case System . Net . HttpStatusCode . BadRequest :
92- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Request error. See response body for details.") ;
70+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Request error. See response body for details.") ;
9371 break ;
9472 case System . Net . HttpStatusCode . Unauthorized :
95- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Authentication failure, invalid access credentials.") ;
73+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Authentication failure, invalid access credentials.") ;
9674 break ;
9775 case System . Net . HttpStatusCode . PaymentRequired :
98- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: In-use Splunk Enterprise license disables this feature.") ;
76+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: In-use Splunk Enterprise license disables this feature.") ;
9977 break ;
10078 case System . Net . HttpStatusCode . Forbidden :
101- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Insufficient permission.") ;
79+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Insufficient permission.") ;
10280 break ;
10381 case System . Net . HttpStatusCode . NotFound :
104- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Requested endpoint does not exist.") ;
82+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Requested endpoint does not exist.") ;
10583 break ;
10684 case System . Net . HttpStatusCode . Conflict :
107- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Invalid operation for this endpoint. See response body for details.") ;
85+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Invalid operation for this endpoint. See response body for details.") ;
10886 break ;
10987 case System . Net . HttpStatusCode . InternalServerError :
110- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Unspecified internal server error. See response body for details.") ;
88+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Unspecified internal server error. See response body for details.") ;
11189 break ;
11290 case System . Net . HttpStatusCode . ServiceUnavailable :
113- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Feature is disabled in configuration file.") ;
91+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Feature is disabled in configuration file.") ;
11492 break ;
115- }
93+ }
11694 }
11795 else if ( responseMessageTask . IsCanceled )
118- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Canceled") ;
96+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Canceled") ;
11997 else
120- Debug . WriteLine ( $ "Splunk HEC { loggerType } Status: Error " + responseMessageTask . Exception != null ? responseMessageTask . Exception . ToString ( ) : "" ) ;
98+ Debug . WriteLine ( $ "[DEBUG] Splunk HEC { loggerType } Status: Error " + responseMessageTask . Exception != null ? responseMessageTask . Exception . ToString ( ) : "" ) ;
99+ }
100+
101+ Uri GetSplunkCollectorUrl ( SplunkLoggerConfiguration configuration , string endPointCustomization )
102+ {
103+ var splunkCollectorUrl = configuration . HecConfiguration . SplunkCollectorUrl ;
104+ if ( ! splunkCollectorUrl . EndsWith ( "/" , StringComparison . InvariantCulture ) )
105+ splunkCollectorUrl = splunkCollectorUrl + "/" ;
106+
107+ if ( ! string . IsNullOrWhiteSpace ( endPointCustomization ) )
108+ splunkCollectorUrl = splunkCollectorUrl + endPointCustomization ;
109+
110+ if ( configuration . HecConfiguration . ChannelIdType == HECConfiguration . ChannelIdOption . QueryString )
111+ splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid . NewGuid ( ) . ToString ( ) ;
112+
113+ if ( configuration . HecConfiguration . UseAuthTokenAsQueryString )
114+ {
115+ var tokenParameter = "token=" + configuration . HecConfiguration . Token ;
116+ splunkCollectorUrl = string . Format ( "{0}{1}{2}" , splunkCollectorUrl , splunkCollectorUrl . Contains ( "?" ) ? "&" : "?" , tokenParameter ) ;
117+ }
118+
119+ return new Uri ( splunkCollectorUrl ) ;
121120 }
122121 }
123122}
0 commit comments