Skip to content

Commit ad47d43

Browse files
authored
Merge pull request #49 from vtex/feature/code_improvement
Feature/code improvement
2 parents 44ab03d + 679fcae commit ad47d43

15 files changed

+81
-116
lines changed

src/SampleWebAPI/Controllers/ValuesController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Logging;
55

6-
namespace Vtex.SampleWebAPI.Controllers
6+
namespace Splunk.SampleWebAPI.Controllers
77
{
88
[Route("api/[controller]")]
99
public class ValuesController : Controller
@@ -20,7 +20,7 @@ public ValuesController(ILoggerFactory loggerFactory)
2020
public IEnumerable<string> Get()
2121
{
2222
var exception = new NotImplementedException();
23-
logger.Log(LogLevel.Critical, new EventId(-1, "Values Controller"), new { route = "Get" }, exception,
23+
logger.Log(LogLevel.Trace, new EventId(-1, "Values Controller"), new { route = "Get" }, exception,
2424
(argState, argException) => {
2525
return string.Format("{0} {1}",
2626
argState != null ? argState.ToString() : string.Empty,

src/SampleWebAPI/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore;
22
using Microsoft.AspNetCore.Hosting;
33

4-
namespace Vtex.SampleWebAPI
4+
namespace Splunk.SampleWebAPI
55
{
66
public class Program
77
{

src/SampleWebAPI/SampleWebAPI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<TargetFramework>netcoreapp2.0</TargetFramework>
55
<ReleaseVersion>1.0.5</ReleaseVersion>
66
<Description>Sample Web API project created to show how to use SplunkLogger library</Description>
7-
<RootNamespace>Vtex.SampleWebAPI</RootNamespace>
7+
<RootNamespace>Splunk.SampleWebAPI</RootNamespace>
88
</PropertyGroup>
99

1010
<ItemGroup>
1111
<Folder Include="wwwroot\" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
15+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

src/SampleWebAPI/Startup.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Logging;
66
using Microsoft.Extensions.Options;
7-
using Splunk;
87
using Splunk.Configurations;
98

10-
namespace Vtex.SampleWebAPI
9+
namespace Splunk.SampleWebAPI
1110
{
1211
public class Startup
1312
{
@@ -36,13 +35,15 @@ public void ConfigureServices(IServiceCollection services)
3635
/// <param name="loggerFactory">Logger factory.</param>
3736
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3837
{
39-
//loggerFactory.AddDebug();
40-
41-
var splunkLoggerConfigurationOption = app.ApplicationServices.GetService<IOptions<SplunkLoggerConfiguration>>();
42-
38+
4339
/******************************** Define Your Logger *********************************/
4440
/* */
45-
loggerFactory.AddHECRawSplunkLogger(splunkLoggerConfigurationOption.Value); //
41+
// Get Configuration to be used at Logger //
42+
var splunkLoggerConfiguration = GetSplunkLoggerConfiguration(app);
43+
// //
44+
// Choose one or more of those loggers //
45+
// //
46+
loggerFactory.AddHECRawSplunkLogger(splunkLoggerConfiguration); //
4647
// //
4748
// //
4849
//loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration); //

src/SampleWebAPI/appsettings.Development.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"LogLevel": {
55
"Default": "Debug",
66
"System": "Information",
7-
"Microsoft": "Information"
7+
"Microsoft": "Information",
8+
"Splunk": "Trace"
89
}
910
},
1011
"Splunk": {
@@ -19,8 +20,8 @@
1920
},
2021
"SocketConfiguration": {
2122
"HostName": "localhost",
22-
"Port": 4242,
23-
"Threshold": "Warning"
24-
}
23+
"Port": 4242
24+
},
25+
"Threshold": "Trace"
2526
}
2627
}

src/SampleWebAPI/appsettings.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"Console": {
1010
"LogLevel": {
11-
"Default": "Warning"
11+
"Default": "Warning",
12+
"Splunk": "Trace"
1213
}
1314
},
1415
"Splunk": {
@@ -23,9 +24,9 @@
2324
},
2425
"SocketConfiguration": {
2526
"HostName": "localhost",
26-
"Port": 4242,
27-
"Threshold": "Warning"
28-
}
27+
"Port": 4242
28+
},
29+
"Threshold": "Trace"
2930
}
3031
}
3132
}

src/SplunkLogger/Loggers/BaseLogger.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Splunk.Loggers
66
/// <summary>
77
/// Define a base logger class.
88
/// </summary>
9-
public abstract class BaseLogger
9+
public abstract class BaseLogger : ILogger
1010
{
1111
protected readonly ILoggerFormatter loggerFormatter;
1212

@@ -43,5 +43,7 @@ public IDisposable BeginScope<T>(T state)
4343
{
4444
return null;
4545
}
46+
47+
public abstract void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);
4648
}
4749
}

src/SplunkLogger/Loggers/HECJsonLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public HECJsonLogger(string categoryName, LogLevel threshold, HttpClient httpCli
3232
/// <param name="state">Log object state.</param>
3333
/// <param name="exception">Log Exception.</param>
3434
/// <param name="formatter">Log text formatter function.</param>
35-
public void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
35+
public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
3636
{
3737
SplunkJSONEntry formatedMessage = null;
3838
if (loggerFormatter != null)

src/SplunkLogger/Loggers/HECRawLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public HECRawLogger(string categoryName, LogLevel threshold, HttpClient httpClie
3131
/// <param name="state">Log object state.</param>
3232
/// <param name="exception">Log Exception.</param>
3333
/// <param name="formatter">Log text formatter function.</param>
34-
public void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
34+
public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
3535
{
3636
string formatedMessage = string.Empty;
3737
if (loggerFormatter != null)

src/SplunkLogger/Loggers/TcpLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public TcpLogger(string categoryName, LogLevel threshold, TcpClient tcpClient, I
3434
/// <param name="state">Log object state.</param>
3535
/// <param name="exception">Log Exception.</param>
3636
/// <param name="formatter">Log text formatter function.</param>
37-
public void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
37+
public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
3838
{
3939
string formatedMessage = string.Empty;
4040
if (loggerFormatter != null)

src/SplunkLogger/Loggers/UdpLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public UdpLogger(string categoryName, LogLevel threshold, UdpClient udpClient, I
3434
/// <param name="state">Log object state.</param>
3535
/// <param name="exception">Log Exception.</param>
3636
/// <param name="formatter">Log text formatter function.</param>
37-
public void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
37+
public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
3838
{
3939
string formatedMessage = string.Empty;
4040
if (loggerFormatter != null)

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
}

src/SplunkLogger/SplunkLogger.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</ItemGroup>
2828
<ItemGroup>
2929
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
30-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
31-
<PackageReference Include="NuGet.Build.Packaging" Version="0.2.0" />
30+
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
31+
<PackageReference Include="NuGet.Build.Packaging" Version="0.2.2" />
3232
</ItemGroup>
3333
</Project>

0 commit comments

Comments
 (0)