SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app) { SplunkLoggerConfiguration result = null; string splunkCollectorUrl = Configuration["SPLUNK_COLLECTOR_URL"]; if (!string.IsNullOrEmpty(splunkCollectorUrl)) { string splunkToken = Configuration["SPLUNK_TOKEN"]; if (!string.IsNullOrEmpty(splunkToken)) { result = new SplunkLoggerConfiguration() { HecConfiguration = new HECConfiguration() { BatchIntervalInMilliseconds = 5000, BatchSizeCount = 10, ChannelIdType = HECConfiguration.ChannelIdOption.None, DefaultTimeoutInMilliseconds = 10000, SplunkCollectorUrl = splunkCollectorUrl, Token = splunkToken, UseAuthTokenAsQueryString = false } }; } } return(result); }
/// <summary> /// Demonstrate how can you provide configuration to your splunk logger addapter(s) /// </summary> SplunkLoggerConfiguration GetSplunkLoggerConfiguration(IApplicationBuilder app) { SplunkLoggerConfiguration result = null; //Retrieving Splunk configuration from appsettings json configuration file var splunkLoggerConfigurationOption = app.ApplicationServices.GetService <IOptions <SplunkLoggerConfiguration> >(); if (splunkLoggerConfigurationOption != null && splunkLoggerConfigurationOption.Value != null) { result = app.ApplicationServices.GetService <IOptions <SplunkLoggerConfiguration> >().Value; } //You can also provide a hard code configuration //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); }
/// <summary> /// Initializes a new instance of the <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> class. /// </summary> /// <param name="configuration">Splunk configuration instance for HEC.</param> /// <param name="loggerFormatter">Formatter instance.</param> public SplunkHECRawLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null) : base(configuration, "raw") { this.loggerFormatter = loggerFormatter; loggers = new ConcurrentDictionary <string, ILogger>(); batchManager = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMilliseconds, Emit); }
Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPointCustomization) { var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl; if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture)) { splunkCollectorUrl = splunkCollectorUrl + "/"; } if (!string.IsNullOrWhiteSpace(endPointCustomization)) { splunkCollectorUrl = splunkCollectorUrl + endPointCustomization; } if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString) { splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid.NewGuid().ToString(); } if (configuration.HecConfiguration.UseAuthTokenAsQueryString) { var tokenParameter = "token=" + configuration.HecConfiguration.Token; splunkCollectorUrl = string.Format("{0}{1}{2}", splunkCollectorUrl, splunkCollectorUrl.Contains("?") ? "&" : "?", tokenParameter); } return(new Uri(splunkCollectorUrl)); }
void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization) { httpClient = new HttpClient { BaseAddress = GetSplunkCollectorUrl(configuration, endPointCustomization) }; if (configuration.HecConfiguration.DefaultTimeoutInMilliseconds > 0) { httpClient.Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMilliseconds); } httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token); if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.RequestHeader) { httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString()); } if (configuration.HecConfiguration.CustomHeaders != null && configuration.HecConfiguration.CustomHeaders.Count > 0) { configuration.HecConfiguration.CustomHeaders.ToList().ForEach(keyValuePair => httpClient.DefaultRequestHeaders.Add(keyValuePair.Key, keyValuePair.Value)); } }
/// <summary> /// Add <see cref="T:Splunk.Providers.SplunkUdpLoggerProvider"/> as provider to logger factory. /// </summary> /// <param name="loggerFactory">Logger factory.</param> /// <param name="configuration">Configuration.</param> /// <param name="formatter">Custom text formatter.</param> public static ILoggerFactory AddUdpSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null) { if (formatter == null) { formatter = DefaultLoggerFormatter; } loggerFactory.AddProvider(new SplunkUdpLoggerProvider(configuration, formatter)); return(loggerFactory); }
/// <summary> /// Initializes a new instance of the <see cref="T:Splunk.Providers.SplunkTcpLoggerProvider"/> class. /// </summary> /// <param name="configuration">Splunk configuration instance for Socket.</param> /// <param name="loggerFormatter">Formatter instance.</param> public SplunkTcpLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null) { this.loggerFormatter = loggerFormatter; this.configuration = configuration; loggers = new ConcurrentDictionary <string, ILogger>(); }
public SplunkHECBaseProvider(SplunkLoggerConfiguration configuration, string endPointCustomization) { SetupHttpClient(configuration, endPointCustomization); }