Skip to content

Commit 4c43d5c

Browse files
authored
Merge pull request #56 from vtex/documentation/improvements
Documentation/improvements
2 parents 1122fc6 + c147671 commit 4c43d5c

File tree

4 files changed

+120
-78
lines changed

4 files changed

+120
-78
lines changed

README.md

Lines changed: 114 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# SplunkLogger
2-
This is a C# .Net Core 2 Splunk ILogger implementation developed at **VTEX** to send log to **Splunk**
32

4-
| Package Name | Release |
5-
|--------------------------------|-----------------|
6-
| `SplunkLogger` | [![NuGet](https://img.shields.io/nuget/v/SplunkLogger.svg)](https://www.nuget.org/packages/SplunkLogger/) |
7-
8-
It was developed to be integrated to .Net Core 2 logging abstractions.
3+
This is a C# .Net Core 2 ILogger implementation developed by **VTEX** developer [Caldas](https://github.com/Caldas) to send data to Splunk.
94

105
### Features
116

12-
* ILoggers **HEC** (*Raw* and *Json*) and **Socket** (*TCP* and *UDP*)
13-
* Batch Manager class (Improve **Splunk** *HEC* performance sending data as batch)
7+
* Multiples ILoggers to send data via **Http** or **Socket**
8+
* **Http** loggers available to send data via **Raw** or **Json** routes
9+
* **Socket** loggers available to send data via **TCP** or **UDP**
10+
* Send **Http** events as batch (Improve **Splunk** *HEC* performance sending data as batch)
11+
12+
### NuGet Package Status
13+
14+
| Package Name | Release |
15+
|--------------------------------|-----------------|
16+
| `SplunkLogger` | [![NuGet](https://img.shields.io/nuget/v/SplunkLogger.svg)](https://www.nuget.org/packages/SplunkLogger/) |
1417

1518
## Usage
1619

@@ -19,103 +22,133 @@ Add *SplunkLogger* nuget library
1922
PM> Install-Package SplunkLogger
2023
```
2124

22-
Initialize a new *SplunkLoggerConfiguration* and the logger provider at **Configure** method at **Startup** class:
25+
### Configure Logger
2326

27+
Let's say for instance that you are creating a WebAPI project, so the first step is to configure one of the Splunk loggers options:
2428
```csharp
25-
static readonly ILoggerFormatter formatter = null;
26-
2729
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
2830
{
29-
var splunkConfiguration = new SplunkLoggerConfiguration()
30-
{
31-
HecConfiguration = new HECConfiguration()
32-
{
33-
SplunkCollectorUrl = "https://localhost:8088/services/collector",
34-
Token = "753c5a9c-fb59-4da0-9064-947f99dc20ba"
35-
}
36-
};
37-
loggerFactory.AddHECRawSplunkLogger(splunkConfiguration, formatter);
38-
app.UseMvc();
31+
var splunkLoggerConfiguration = GetSplunkLoggerConfiguration(app);
32+
33+
//Append Http Raw logger
34+
//loggerFactory.AddHECRawSplunkLogger(splunkLoggerConfiguration);
35+
36+
//Append Http Json logger
37+
loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration);
38+
39+
//Append Socket TCP logger
40+
//loggerFactory.AddTcpSplunkLogger(splunkConfiguration);
41+
42+
//Append Socket UDP logger
43+
//loggerFactory.AddUdpSplunkLogger(splunkConfiguration);
3944
}
4045
```
4146

42-
The case above is using a **Splunk** *HEC Raw* logger without any custom formatter as log provider.
47+
As you can see, no matter what is your option you always must delivery a *SplunkLoggerConfiguration* when adding a ILogger to the logger factory. You can provide it via config or as hard code:
4348

44-
Now, in your controller you can log normally, for instance:
49+
#### Get Configuration From Json File
4550

46-
```csharp
47-
[Route("api/[controller]")]
48-
public class ValuesController : Controller
49-
{
50-
readonly ILogger logger;
51+
You can provide the configuration from json file using .Net Core 2 configuration binding feature.
5152

52-
public ValuesController(ILoggerFactory loggerFactory)
53-
{
54-
logger = loggerFactory.CreateLogger<ValuesController>();
55-
}
53+
For instance at [SampleWebAPI project](https://github.com/vtex/SplunkLogger/tree/master/src/SampleWebAPI) we use the *appsettings.json* file.
5654

57-
// GET api/values
58-
[HttpGet]
59-
public IEnumerable<string> Get()
60-
{
61-
logger.LogCritical(new EventId(-1, "Values Controller"), new NotImplementedException(), "Error on GET api/values route");
62-
return new string[] { "value1", "value2" };
55+
```json
56+
{
57+
"Logging": {
58+
"IncludeScopes": false,
59+
"LogLevel": {
60+
"Default": "Trace",
61+
"System": "Debug",
62+
"Microsoft": "Debug",
63+
"Splunk": "Trace"
64+
}
65+
},
66+
"Splunk": {
67+
"HecConfiguration": {
68+
"BatchIntervalInMilliseconds": 5000,
69+
"BatchSizeCount": 10,
70+
"ChannelIdType": "None",
71+
"DefaultTimeoutInMiliseconds": 10000,
72+
"SplunkCollectorUrl": "https://localhost:8088/services/collector/",
73+
"Token": "753c5a9c-fb59-4da0-9064-947f99dc20ba",
74+
"UseAuthTokenAsQueryString": false
75+
},
76+
"SocketConfiguration": {
77+
"HostName": "localhost",
78+
"Port": 4242
6379
}
80+
}
6481
}
6582
```
6683

67-
-------------------------------------------
68-
69-
# VTEXSplunkLogger
70-
For us at **VTEX** we need more customized log entries at Splunk and also we need easier ways to call for log registration during the code. For that, we created this *VTEXSplunkLogger* library.
71-
72-
This project contains all **VTEX** extra classes designed to ease logging. All classes changes .Net Core 2 log abstraction to a customized log entry at **Splunk**
73-
74-
| Package Name | Release |
75-
|--------------------------------|-----------------|
76-
| `VTEXSplunkLogger` | [![MyGet](https://img.shields.io/myget/vtexlab/v/VTEXSplunkLogger.svg)](https://www.myget.org/feed/vtexlab/package/nuget/VTEXSplunkLogger) |
77-
78-
### Features
79-
80-
* ILoggerExtensions (*Allow easy log creation*)
81-
* LoggerFactoryExtensions (*Simplify loggerFactory add provider method call*)
82-
* VTEXSplunkLoggerFormatter (*Custom `ILoggerFormatter` which creates VTEX's custom text to Splunk*)
83-
84+
If you intend to send data via **Http** you should set **HecConfiguration** section and if you choose to send data via socket you must set **SocketConfiguration** section.
8485

85-
## Usage
86+
Now we need to configure SplunkLoggerConfiguration at dependency injection and indicate to use **Splunk** section from configuration file.
87+
```csharp
88+
/// <summary>
89+
/// This method gets called by the runtime. Use this method to add services to the container.
90+
/// </summary>
91+
public void ConfigureServices(IServiceCollection services)
92+
{
93+
services.Configure<SplunkLoggerConfiguration>(Configuration.GetSection("Splunk"));
94+
services.AddMvc();
95+
}
96+
```
8697

87-
Add *VTEXSplunkLogger* nuget library
88-
```powershell
89-
PM> Install-Package VTEXSplunkLogger -Source https://www.myget.org/F/vtexlab/api/v3/index.json
98+
Now that `SplunkLoggerConfiguration` class is configured we can retrieve it by requesting `IOptions<SplunkLoggerConfiguration>` at dependency injection service, like:
99+
```csharp
100+
/// <summary>
101+
/// Demonstrate how can you provide configuration to your splunk logger addapter(s)
102+
/// </summary>
103+
SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app)
104+
{
105+
SplunkLoggerConfiguration result = null;
106+
var splunkLoggerConfigurationOption = app.ApplicationServices.GetService<IOptions<SplunkLoggerConfiguration>>();
107+
if(splunkLoggerConfigurationOption != null && splunkLoggerConfigurationOption.Value != null)
108+
result = app.ApplicationServices.GetService<IOptions<SplunkLoggerConfiguration>>().Value;
109+
return result;
110+
}
90111
```
91112

92-
Initialize a new *SplunkLoggerConfiguration* and the logger provider at **Configure** method at **Startup** class:
113+
#### Get Static Configuration
93114

94-
```csharp
95-
static readonly ILoggerFormatter formatter = new VTEXSplunkLoggerFormatter();
115+
If you don't want to use the configuration file, you can provide a hard coded configuration instance:
96116

97-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
117+
```csharp
118+
/// <summary>
119+
/// Demonstrate how can you provide configuration to your splunk logger addapter(s)
120+
/// </summary>
121+
SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app)
98122
{
99-
//TODO: Set your project name
100-
ILoggerExtensions.SetApplication("ProjectX");
101-
102-
var splunkConfiguration = new SplunkLoggerConfiguration()
123+
SplunkLoggerConfiguration result = new SplunkLoggerConfiguration()
103124
{
104125
HecConfiguration = new HECConfiguration()
105126
{
106127
SplunkCollectorUrl = "https://localhost:8088/services/collector",
128+
BatchIntervalInMilliseconds = 5000,
129+
BatchSizeCount = 100,
130+
ChannelIdType = HECConfiguration.ChannelIdOption.None,
107131
Token = "753c5a9c-fb59-4da0-9064-947f99dc20ba"
132+
},
133+
SocketConfiguration = new SocketConfiguration()
134+
{
135+
HostName = "localhost",
136+
Port = 8111
108137
}
109138
};
110-
loggerFactory.AddHECRawSplunkLogger(splunkConfiguration, formatter);
111-
app.UseMvc();
139+
return result;
112140
}
113141
```
114142

115-
```csharp
116-
using Vtex;
117-
// other usings ..
143+
Again, if you intend to use send data via **Http** you should set **HecConfiguration** property and if you choose to send data via socket you must set **SocketConfiguration** property.
144+
145+
### Logging
118146

147+
Now that everything is configured and ILoggerFactory already have the desired ILogger instance added you can log your messages.
148+
149+
Here is a sample of how you can log messages, in this case I'm logging a `NotImplementedException` at [SampleWebAPI project](https://github.com/vtex/SplunkLogger/tree/master/src/SampleWebAPI) `ValuesController`.
150+
151+
```csharp
119152
[Route("api/[controller]")]
120153
public class ValuesController : Controller
121154
{
@@ -130,13 +163,16 @@ public class ValuesController : Controller
130163
[HttpGet]
131164
public IEnumerable<string> Get()
132165
{
133-
logger.DefineVTEXLog(LogLevel.Critical,
134-
"Values Controller",
135-
"api/values",
136-
string.Empty,
137-
new NotImplementedException(),
138-
new Tuple<string, string>("method", "GET"));
166+
logger.LogCritical(new EventId(-1, "Values Controller"), new NotImplementedException(), "Error on GET api/values route");
139167
return new string[] { "value1", "value2" };
140168
}
141169
}
142170
```
171+
172+
## More Information
173+
174+
You can read more about the projects and it's details (like send http events as batch) at [Wiki page](https://github.com/vtex/SplunkLogger/wiki)
175+
176+
## Project Sponsored By
177+
178+
![VTEX](https://raw.githubusercontent.com/vtex/SplunkLogger/documentation/improvements/VTEX_icon.png) **[VTEX](https://www.vtex.com)**

VTEX_icon.png

-9.6 KB
Loading

src/SampleWebAPI/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sample WebAPI Project
2+
3+
This folder contains a sample WebAPI project that uses SplunkLogger library to send data to **Splunk**

src/SplunkLogger/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SplunkLogger Library
2+
3+
This folder contains the SplunkLogger library

0 commit comments

Comments
 (0)