Skip to content

Commit 8e34c38

Browse files
authored
Merge pull request #12 from Caldas/birth
Created initial solution and projects. Implemented HEC (Raw and JSON) and Socket (TCP and UDP). Implemented LoggerProviders Documented classes Release Nuget package
2 parents b70b87b + 54e9aa7 commit 8e34c38

30 files changed

+1391
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace VTEX.SampleWebAPI.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
public class ValuesController : Controller
10+
{
11+
readonly ILogger logger;
12+
13+
public ValuesController(ILoggerFactory loggerFactory)
14+
{
15+
logger = loggerFactory.CreateLogger<ValuesController>();
16+
}
17+
18+
// GET api/values
19+
[HttpGet]
20+
public IEnumerable<string> Get()
21+
{
22+
logger.DefineVTEXLog(LogLevel.Critical,
23+
"Values Controller",
24+
"api/values",
25+
string.Empty,
26+
new NotImplementedException(),
27+
new Tuple<string, string>("method", "GET"));
28+
return new string[] { "value1", "value2" };
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Extensions.Logging;
2+
using Splunk.Providers;
3+
using Splunk.Configurations;
4+
using Splunk;
5+
6+
namespace VTEX.SampleWebAPI
7+
{
8+
public static class LoggerFactoryExtensions
9+
{
10+
public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter)
11+
{
12+
loggerFactory.AddProvider(new SplunkHECRawLoggerProvider(configuration, formatter));
13+
return loggerFactory;
14+
}
15+
16+
public static ILoggerFactory AddHECJsonSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter)
17+
{
18+
loggerFactory.AddProvider(new SplunkHECJsonLoggerProvider(configuration, formatter));
19+
return loggerFactory;
20+
}
21+
22+
public static ILoggerFactory AddTcpSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter)
23+
{
24+
loggerFactory.AddProvider(new SplunkTcpLoggerProvider(configuration, formatter));
25+
return loggerFactory;
26+
}
27+
28+
public static ILoggerFactory AddUdpSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter)
29+
{
30+
loggerFactory.AddProvider(new SplunkUdpLoggerProvider(configuration, formatter));
31+
return loggerFactory;
32+
}
33+
}
34+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace VTEX.SampleWebAPI.Logging
3+
{
4+
public enum VTEXEventLevel : int
5+
{
6+
Critical = 3,
7+
Important = 2,
8+
Default = 1,
9+
Debug = 0,
10+
}
11+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+

2+
namespace VTEX.SampleWebAPI.Logging
3+
{
4+
public enum VTEXLogType
5+
{
6+
Error,
7+
Warning,
8+
Info
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace VTEX.SampleWebAPI.Logging
5+
{
6+
public class VTEXSplunkEntry
7+
{
8+
public string WorkflowType { get; private set; }
9+
public string WorkflowInstance { get; private set; }
10+
public string Account { get; private set; }
11+
public Exception Exception { get; private set; }
12+
public List<Tuple<string, string>> ExtraParameters { get; private set; }
13+
14+
public VTEXSplunkEntry(string workflowType, string workflowInstance, string account = "", Exception exception = null, params Tuple<string, string>[] extraParameters)
15+
{
16+
if (string.IsNullOrWhiteSpace(workflowType))
17+
throw new ArgumentNullException(nameof(workflowType));
18+
19+
if (string.IsNullOrWhiteSpace(workflowInstance))
20+
throw new ArgumentNullException(nameof(workflowInstance));
21+
22+
ExtraParameters = new List<Tuple<string, string>>(extraParameters);
23+
WorkflowType = workflowType;
24+
WorkflowInstance = workflowInstance;
25+
Account = account;
26+
Exception = exception;
27+
28+
if (exception != null)
29+
{
30+
ExtraParameters.Add(new Tuple<string, string>("exception_type", exception.GetType().FullName));
31+
ExtraParameters.Add(new Tuple<string, string>("exception_message", exception.Message));
32+
}
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
using VTEX.SampleWebAPI.Logging;
4+
5+
namespace VTEX.SampleWebAPI
6+
{
7+
public static class VTEXSplunkEntryExtensions
8+
{
9+
static readonly EventId EmptyEventId = new EventId();
10+
11+
public static void DefineVTEXLog(this ILogger logger, LogLevel logLevel, string workflowType, string workflowInstance, string account = "", Exception exception = null, params Tuple<string, string>[] extraParameters)
12+
{
13+
string formattedMessage = string.Empty;
14+
logger.Log(logLevel,
15+
EmptyEventId,
16+
new VTEXSplunkEntry(workflowType, workflowInstance, account, exception, extraParameters),
17+
exception, (VTEXSplunkEntry arg1, Exception arg2) =>
18+
{
19+
if (string.IsNullOrWhiteSpace(formattedMessage))
20+
{
21+
var eventSegment = $"Event '{workflowType}' on {workflowInstance}";
22+
var exceptionSegment = "";
23+
if (exception != null)
24+
exceptionSegment = $"Exception type: {exception.GetType().FullName}. Exception message: {exception.Message}";
25+
var accountSegment = !string.IsNullOrWhiteSpace(account) ? account : "-";
26+
formattedMessage = string.Format($"[{logLevel}] {eventSegment} {accountSegment}. {exceptionSegment}");
27+
}
28+
return formattedMessage;
29+
});
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Linq;
3+
using Microsoft.Extensions.Logging;
4+
using Splunk;
5+
6+
namespace VTEX.SampleWebAPI.Logging
7+
{
8+
public class VTEXSplunkLoggerFormatter : ILoggerFormatter
9+
{
10+
const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";
11+
12+
readonly string appVersion;
13+
readonly string host;
14+
15+
public VTEXSplunkLoggerFormatter(string appVersion, string host)
16+
{
17+
this.appVersion = appVersion;
18+
this.host = host;
19+
}
20+
21+
public string Format<T>(LogLevel logLevel, EventId eventId, T state, Exception exception)
22+
{
23+
string log;
24+
if (state is VTEXSplunkEntry)
25+
{
26+
var splunkEntry = state as VTEXSplunkEntry;
27+
var dateTime = DateTime.UtcNow.ToString(DateTimeFormat);
28+
string extraData = string.Empty;
29+
if (splunkEntry.ExtraParameters != null && splunkEntry.ExtraParameters.Count > 0)
30+
extraData = string.Join(" ", splunkEntry.ExtraParameters.Select(part => { return $"{part.Item1}=\"{part.Item2}\""; }));
31+
string account = splunkEntry.Account;
32+
if (string.IsNullOrWhiteSpace(splunkEntry.Account))
33+
account = "-";
34+
log = string.Format($"{dateTime} VTEXLog,splunkmanager,{host},{GetVTEXEventLevel(logLevel)},{GetVTEXLogType(logLevel)},\"{splunkEntry.WorkflowType}\",\"{splunkEntry.WorkflowInstance}\",{account},{appVersion} {extraData}");
35+
}
36+
else
37+
{
38+
var eventSegment = "";
39+
if (!string.IsNullOrWhiteSpace(eventId.Name))
40+
eventSegment = $"Event '{eventId.Name}' on {eventId.Id}";
41+
var exceptionSegment = "";
42+
if (exception != null)
43+
exceptionSegment = $"Exception type: {exception.GetType().FullName}. Exception message: {exception.Message}";
44+
log = string.Format($"[{logLevel}] {eventSegment} {state.ToString()}. {exceptionSegment}");
45+
}
46+
return log;
47+
}
48+
49+
public SplunkJSONEntry FormatJson<T>(LogLevel logLevel, EventId eventId, T state, Exception exception)
50+
{
51+
return new SplunkJSONEntry(Format(logLevel, eventId, state, exception), 0, host, string.Empty, "Log");
52+
}
53+
54+
string GetVTEXEventLevel(LogLevel logLevel)
55+
{
56+
VTEXEventLevel eventLevel = VTEXEventLevel.Debug;
57+
switch (logLevel)
58+
{
59+
case LogLevel.Critical:
60+
eventLevel = VTEXEventLevel.Critical;
61+
break;
62+
case LogLevel.Warning:
63+
case LogLevel.Error:
64+
eventLevel = VTEXEventLevel.Important;
65+
break;
66+
case LogLevel.Information:
67+
eventLevel = VTEXEventLevel.Default;
68+
break;
69+
case LogLevel.Trace:
70+
case LogLevel.Debug:
71+
case LogLevel.None:
72+
eventLevel = VTEXEventLevel.Debug;
73+
break;
74+
}
75+
return eventLevel.ToString().ToLower();
76+
}
77+
78+
string GetVTEXLogType(LogLevel logLevel)
79+
{
80+
VTEXLogType logType = VTEXLogType.Info;
81+
switch (logLevel)
82+
{
83+
case LogLevel.Critical:
84+
case LogLevel.Error:
85+
logType = VTEXLogType.Error;
86+
break;
87+
case LogLevel.Warning:
88+
logType = VTEXLogType.Warning;
89+
break;
90+
case LogLevel.Debug:
91+
case LogLevel.Information:
92+
case LogLevel.Trace:
93+
case LogLevel.None:
94+
logType = VTEXLogType.Info;
95+
break;
96+
}
97+
return logType.ToString().ToLower();
98+
}
99+
}
100+
}

src/SampleWebAPI/Program.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace VTEX.SampleWebAPI
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BuildWebHost(args).Run();
11+
}
12+
13+
public static IWebHost BuildWebHost(string[] args) =>
14+
WebHost.CreateDefaultBuilder(args)
15+
.UseStartup<Startup>()
16+
.Build();
17+
}
18+
}

src/SampleWebAPI/SampleWebAPI.csproj

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<ReleaseVersion>1.0.0</ReleaseVersion>
6+
<Description>Sample Web API project created to show how to use SplunkLogger library</Description>
7+
<RootNamespace>VTEX.SampleWebAPI</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Folder Include="wwwroot\" />
12+
<Folder Include="Logging\" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\SplunkLogger\SplunkLogger.csproj" />
25+
</ItemGroup>
26+
</Project>

0 commit comments

Comments
 (0)