SplunkLogger 1.1.3
dotnet add package SplunkLogger --version 1.1.3
NuGet\Install-Package SplunkLogger -Version 1.1.3
<PackageReference Include="SplunkLogger" Version="1.1.3" />
paket add SplunkLogger --version 1.1.3
#r "nuget: SplunkLogger, 1.1.3"
// Install SplunkLogger as a Cake Addin #addin nuget:?package=SplunkLogger&version=1.1.3 // Install SplunkLogger as a Cake Tool #tool nuget:?package=SplunkLogger&version=1.1.3
SplunkLogger
This is a C# .Net Core 2 ILogger implementation developed by VTEX developer Caldas to send data to Splunk.
Features
- Multiples ILoggers to send data via Http or Socket
- Http loggers available to send data via Raw or Json routes
- Socket loggers available to send data via TCP or UDP
- Send Http events as batch (Improve Splunk HEC performance sending data as batch)
- ILoggerFormatter that enable you to handle and formart your logs before send it to Splunk
NuGet Package Status
Package Name | Release |
---|---|
SplunkLogger |
Usage
Add SplunkLogger nuget library
PM> Install-Package SplunkLogger
Configure Logger
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:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var splunkLoggerConfiguration = GetSplunkLoggerConfiguration(app);
//Append Http Raw logger
//loggerFactory.AddHECRawSplunkLogger(splunkLoggerConfiguration);
//Append Http Json logger
loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration);
//Append Socket TCP logger
//loggerFactory.AddTcpSplunkLogger(splunkConfiguration);
//Append Socket UDP logger
//loggerFactory.AddUdpSplunkLogger(splunkConfiguration);
}
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:
Get Configuration From Json File
You can provide the configuration from json file using .Net Core 2 configuration binding feature.
For instance at SampleWebAPI project we use the appsettings.json file.
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Debug",
"Microsoft": "Debug",
"Splunk": "Trace"
}
},
"Splunk": {
"HecConfiguration": {
"BatchIntervalInMilliseconds": 5000,
"BatchSizeCount": 10,
"ChannelIdType": "None",
"DefaultTimeoutInMiliseconds": 10000,
"SplunkCollectorUrl": "https://localhost:8088/services/collector/",
"Token": "753c5a9c-fb59-4da0-9064-947f99dc20ba",
"UseAuthTokenAsQueryString": false
},
"SocketConfiguration": {
"HostName": "localhost",
"Port": 4242
}
}
}
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.
Now we need to configure SplunkLoggerConfiguration at dependency injection and indicate to use Splunk section from configuration file.
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SplunkLoggerConfiguration>(Configuration.GetSection("Splunk"));
services.AddMvc();
}
Now that SplunkLoggerConfiguration
class is configured we can retrieve it by requesting IOptions<SplunkLoggerConfiguration>
at dependency injection service, like:
/// <summary>
/// Demonstrate how can you provide configuration to your splunk logger addapter(s)
/// </summary>
SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app)
{
SplunkLoggerConfiguration result = null;
var splunkLoggerConfigurationOption = app.ApplicationServices.GetService<IOptions<SplunkLoggerConfiguration>>();
if(splunkLoggerConfigurationOption != null && splunkLoggerConfigurationOption.Value != null)
result = app.ApplicationServices.GetService<IOptions<SplunkLoggerConfiguration>>().Value;
return result;
}
Get Static Configuration
If you don't want to use the configuration file, you can provide a hard coded configuration instance:
/// <summary>
/// Demonstrate how can you provide configuration to your splunk logger addapter(s)
/// </summary>
SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app)
{
SplunkLoggerConfiguration result = new SplunkLoggerConfiguration()
{
HecConfiguration = new HECConfiguration()
{
SplunkCollectorUrl = "https://localhost:8088/services/collector",
BatchIntervalInMilliseconds = 5000,
BatchSizeCount = 100,
ChannelIdType = HECConfiguration.ChannelIdOption.None,
Token = "753c5a9c-fb59-4da0-9064-947f99dc20ba"
},
SocketConfiguration = new SocketConfiguration()
{
HostName = "localhost",
Port = 8111
}
};
return result;
}
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.
Logging
Now that everything is configured and ILoggerFactory already have the desired ILogger instance added you can log your messages.
Here is a sample of how you can log messages, in this case I'm logging a NotImplementedException
at SampleWebAPI project ValuesController
.
[Route("api/[controller]")]
public class ValuesController : Controller
{
readonly ILogger logger;
public ValuesController(ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<ValuesController>();
}
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
var exception = new NotImplementedException();
var message = "An error has ocurried route=Get";
var eventId = new EventId(-1, "Values Controller");
//You can log like this
logger.Log(LogLevel.Trace, eventId, message, exception);
//Or like this
//logger.LogTrace(eventId, exception, message);
return new string[] { "4", "2" };
}
}
More Information
You can read more about the projects and it's details (like send http events as batch) at Wiki page
Project Sponsored By
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- Microsoft.Extensions.Logging (>= 2.0.0)
- Newtonsoft.Json (>= 11.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.3 | 73,619 | 5/21/2018 |
1.1.2 | 1,171 | 4/20/2018 |
1.1.1 | 1,890 | 3/15/2018 |
1.1.0 | 1,078 | 2/26/2018 |
1.0.8 | 999 | 2/23/2018 |
1.0.7 | 934 | 2/22/2018 |
1.0.6 | 1,033 | 2/22/2018 |
1.0.5 | 1,009 | 2/21/2018 |
1.0.4 | 898 | 2/21/2018 |
1.0.3 | 1,652 | 1/11/2018 |
1.0.2 | 1,222 | 1/11/2018 |
1.0.1 | 1,172 | 1/10/2018 |
1.0.0 | 1,266 | 1/10/2018 |