Skip to content

Commit 679fcae

Browse files
committed
Simplified debug from splunk response
#44
1 parent 1e785e6 commit 679fcae

File tree

3 files changed

+49
-89
lines changed

3 files changed

+49
-89
lines changed

src/SplunkLogger/Providers/SplunkHECBaseProvider.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net.Http;
34
using System.Net.Http.Headers;
5+
using System.Threading.Tasks;
46
using Microsoft.Extensions.Logging;
57
using Splunk.Configurations;
68

@@ -73,5 +75,49 @@ Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPoi
7375

7476
return new Uri(splunkCollectorUrl);
7577
}
78+
79+
protected void DebugSplunkResponse(Task<HttpResponseMessage> responseMessageTask, string loggerType)
80+
{
81+
if (responseMessageTask.IsCompletedSuccessfully)
82+
{
83+
switch (responseMessageTask.Result.StatusCode)
84+
{
85+
case System.Net.HttpStatusCode.OK:
86+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Request completed successfully.");
87+
break;
88+
case System.Net.HttpStatusCode.Created:
89+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Create request completed successfully.");
90+
break;
91+
case System.Net.HttpStatusCode.BadRequest:
92+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Request error. See response body for details.");
93+
break;
94+
case System.Net.HttpStatusCode.Unauthorized:
95+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Authentication failure, invalid access credentials.");
96+
break;
97+
case System.Net.HttpStatusCode.PaymentRequired:
98+
Debug.WriteLine($"Splunk HEC {loggerType} Status: In-use Splunk Enterprise license disables this feature.");
99+
break;
100+
case System.Net.HttpStatusCode.Forbidden:
101+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Insufficient permission.");
102+
break;
103+
case System.Net.HttpStatusCode.NotFound:
104+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Requested endpoint does not exist.");
105+
break;
106+
case System.Net.HttpStatusCode.Conflict:
107+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Invalid operation for this endpoint. See response body for details.");
108+
break;
109+
case System.Net.HttpStatusCode.InternalServerError:
110+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Unspecified internal server error. See response body for details.");
111+
break;
112+
case System.Net.HttpStatusCode.ServiceUnavailable:
113+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Feature is disabled in configuration file.");
114+
break;
115+
}
116+
}
117+
else if (responseMessageTask.IsCanceled)
118+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Canceled");
119+
else
120+
Debug.WriteLine($"Splunk HEC {loggerType} Status: Error " + responseMessageTask.Exception != null ? responseMessageTask.Exception.ToString() : "");
121+
}
76122
}
77123
}

src/SplunkLogger/Providers/SplunkHECJsonLoggerProvider.cs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System;
2-
using System.Collections.Concurrent;
1+
using System.Collections.Concurrent;
32
using System.Collections.Generic;
4-
using System.Diagnostics;
53
using System.Linq;
64
using System.Net.Http;
7-
using System.Net.Http.Headers;
85
using System.Text;
96
using Microsoft.Extensions.Logging;
107
using Newtonsoft.Json;
@@ -85,48 +82,7 @@ public void Emit(List<object> events)
8582
var formatedMessage = string.Join(" ", jArray);
8683
var stringContent = new StringContent(formatedMessage, Encoding.UTF8, "application/json");
8784
httpClient.PostAsync(string.Empty, stringContent)
88-
.ContinueWith(task => {
89-
if (task.IsCompletedSuccessfully)
90-
switch (task.Result.StatusCode)
91-
{
92-
case System.Net.HttpStatusCode.OK:
93-
Debug.WriteLine("Splunk HEC JSON Status: Request completed successfully.");
94-
break;
95-
case System.Net.HttpStatusCode.Created:
96-
Debug.WriteLine("Splunk HEC JSON Status: Create request completed successfully.");
97-
break;
98-
case System.Net.HttpStatusCode.BadRequest:
99-
Debug.WriteLine("Splunk HEC JSON Status: Request error. See response body for details.");
100-
break;
101-
case System.Net.HttpStatusCode.Unauthorized:
102-
Debug.WriteLine("Splunk HEC JSON Status: Authentication failure, invalid access credentials.");
103-
break;
104-
case System.Net.HttpStatusCode.PaymentRequired:
105-
Debug.WriteLine("Splunk HEC JSON Status: In-use Splunk Enterprise license disables this feature.");
106-
break;
107-
case System.Net.HttpStatusCode.Forbidden:
108-
Debug.WriteLine("Splunk HEC JSON Status: Insufficient permission.");
109-
break;
110-
case System.Net.HttpStatusCode.NotFound:
111-
Debug.WriteLine("Splunk HEC JSON Status: Requested endpoint does not exist.");
112-
break;
113-
case System.Net.HttpStatusCode.Conflict:
114-
Debug.WriteLine("Splunk HEC JSON Status: Invalid operation for this endpoint. See response body for details.");
115-
break;
116-
case System.Net.HttpStatusCode.InternalServerError:
117-
Debug.WriteLine("Splunk HEC JSON Status: Unspecified internal server error. See response body for details.");
118-
break;
119-
case System.Net.HttpStatusCode.ServiceUnavailable:
120-
Debug.WriteLine("Splunk HEC JSON Status: Feature is disabled in configuration file.");
121-
break;
122-
default:
123-
break;
124-
}
125-
else if (task.IsCanceled)
126-
Debug.WriteLine("Splunk HEC RAW Status: Canceled");
127-
else
128-
Debug.WriteLine("Splunk HEC RAW Status: Error " + task.Exception != null ? task.Exception.ToString() : "");
129-
});
85+
.ContinueWith(task => DebugSplunkResponse(task, "json"));
13086
}
13187
}
13288
}

src/SplunkLogger/Providers/SplunkHECRawLoggerProvider.cs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
54
using System.Linq;
65
using System.Net.Http;
76
using Microsoft.Extensions.Logging;
@@ -81,48 +80,7 @@ public void Emit(List<object> events)
8180
var formatedMessage = string.Join(Environment.NewLine, events.Select(evt => evt.ToString()));
8281
var stringContent = new StringContent(formatedMessage);
8382
httpClient.PostAsync(string.Empty, stringContent)
84-
.ContinueWith(task => {
85-
if (task.IsCompletedSuccessfully)
86-
switch (task.Result.StatusCode)
87-
{
88-
case System.Net.HttpStatusCode.OK:
89-
Debug.WriteLine("Splunk HEC RAW Status: Request completed successfully.");
90-
break;
91-
case System.Net.HttpStatusCode.Created:
92-
Debug.WriteLine("Splunk HEC RAW Status: Create request completed successfully.");
93-
break;
94-
case System.Net.HttpStatusCode.BadRequest:
95-
Debug.WriteLine("Splunk HEC RAW Status: Request error. See response body for details.");
96-
break;
97-
case System.Net.HttpStatusCode.Unauthorized:
98-
Debug.WriteLine("Splunk HEC RAW Status: Authentication failure, invalid access credentials.");
99-
break;
100-
case System.Net.HttpStatusCode.PaymentRequired:
101-
Debug.WriteLine("Splunk HEC RAW Status: In-use Splunk Enterprise license disables this feature.");
102-
break;
103-
case System.Net.HttpStatusCode.Forbidden:
104-
Debug.WriteLine("Splunk HEC RAW Status: Insufficient permission.");
105-
break;
106-
case System.Net.HttpStatusCode.NotFound:
107-
Debug.WriteLine("Splunk HEC RAW Status: Requested endpoint does not exist.");
108-
break;
109-
case System.Net.HttpStatusCode.Conflict:
110-
Debug.WriteLine("Splunk HEC RAW Status: Invalid operation for this endpoint. See response body for details.");
111-
break;
112-
case System.Net.HttpStatusCode.InternalServerError:
113-
Debug.WriteLine("Splunk HEC RAW Status: Unspecified internal server error. See response body for details.");
114-
break;
115-
case System.Net.HttpStatusCode.ServiceUnavailable:
116-
Debug.WriteLine("Splunk HEC RAW Status: Feature is disabled in configuration file.");
117-
break;
118-
default:
119-
break;
120-
}
121-
else if (task.IsCanceled)
122-
Debug.WriteLine("Splunk HEC RAW Status: Canceled");
123-
else
124-
Debug.WriteLine("Splunk HEC RAW Status: Error " + task.Exception != null ? task.Exception.ToString() : "");
125-
});
83+
.ContinueWith(task => DebugSplunkResponse(task, "raw"));
12684
}
12785
}
12886
}

0 commit comments

Comments
 (0)