/// <summary> /// Initializes the target. Can be used by inheriting classes /// to initialize logging. /// </summary> /// <exception cref="NLogConfigurationException"> /// SplunkHttpEventCollector ServerUrl is not set! /// or /// SplunkHttpEventCollector Token is not set! /// </exception> protected override void InitializeTarget() { base.InitializeTarget(); NLog.Common.InternalLogger.Debug("Initializing SplunkHttpEventCollector"); if (string.IsNullOrEmpty(ServerUrl?.Authority)) { throw new NLogConfigurationException("SplunkHttpEventCollector ServerUrl is not set!"); } if (string.IsNullOrEmpty(Token)) { throw new NLogConfigurationException("SplunkHttpEventCollector Token is not set!"); } _metaData.Clear(); _hecSender = new HttpEventCollectorSender( ServerUrl, // Splunk HEC URL Token, // Splunk HEC token *GUID* Channel, // Splunk HEC data channel *GUID* GetMetaData(Index, Source, SourceType), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order BatchSizeBytes == 0 && BatchSizeCount == 0 ? 0 : 250, // BatchInterval - Set to 0 to disable BatchSizeBytes, // BatchSizeBytes - Set to 0 to disable BatchSizeCount, // BatchSizeCount - Set to 0 to disable IgnoreSslErrors, // Enable Ssl Error ignore for self singed certs *BOOL* UseProxy, // UseProxy - Set to false to disable MaxConnectionsPerServer, new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin, // Resend Middleware with retry httpVersion10Hack: UseHttpVersion10Hack ); _hecSender.OnError += (e) => { InternalLogger.Error(e, "SplunkHttpEventCollector(Name={0}): Failed to send LogEvents", Name); }; }
protected override void InitializeTarget() { base.InitializeTarget(); if (string.IsNullOrEmpty(ServerUrl?.Authority)) { throw new NLogConfigurationException("SplunkHttpEventCollector ServerUrl is not set!"); } if (string.IsNullOrEmpty(Token)) { throw new NLogConfigurationException("SplunkHttpEventCollector Token is not set!"); } _hecSender = new HttpEventCollectorSender( ServerUrl, // Splunk HEC URL Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(Index, Source, (SourceType ?? "_json"), GetMachineName()), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order 0, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable 0, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin // Resend Middleware with retry ); }
public async Task <IHttpActionResult> SendDataToSplunk() { string message = "some message"; var middleware = new HttpEventCollectorResendMiddleware(100); // preparing parameters Uri uri = new Uri("http://localhost:8088"); string token = "73f4c508-35da-4d14-9d4b-12a8d820935f"; try { var ecSender = new HttpEventCollectorSender(uri, token, null, HttpEventCollectorSender.SendMode.Sequential, 0, 0, 0, middleware.Plugin); ecSender.OnError += EcSender_OnError; ecSender.Send(Guid.NewGuid().ToString(), "INFO", null, new { Foo = message }); var flushTask = ecSender.FlushAsync(); flushTask.Start(); await flushTask; return(Ok("data saved to splunk")); } catch (Exception ex) { return(Ok(ex.Message)); } }
protected override void InitializeTarget() { base.InitializeTarget(); if (string.IsNullOrEmpty(ServerUrl?.Authority)) { throw new NLogConfigurationException("SplunkHttpEventCollector ServerUrl is not set!"); } if (string.IsNullOrEmpty(Token)) { throw new NLogConfigurationException("SplunkHttpEventCollector Token is not set!"); } _hecSender = new HttpEventCollectorSender( ServerUrl, // Splunk HEC URL Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(null, null, "_json", GetMachineName()), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order 0, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable 0, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin // Resend Middleware with retry ); // throw error on send failure _hecSender.OnError += exception => { throw new NLogRuntimeException($"SplunkHttpEventCollector failed to send log event to Splunk server '{ServerUrl?.Authority}' using token '{Token}'. Exception: {exception}"); }; }
public SplunkLogger() { splunkServerUri = new Uri("http://splunk.sundman.at:8088"); middleware = new HttpEventCollectorResendMiddleware(100); sender = new HttpEventCollectorSender(splunkServerUri, token, null, HttpEventCollectorSender.SendMode.Parallel, 0, 0, 0, middleware.Plugin, Formatter); sender.OnError += onError; }
/// <summary> /// Initializes the target. Can be used by inheriting classes /// to initialize logging. /// </summary> /// <exception cref="NLogConfigurationException"> /// SplunkHttpEventCollector ServerUrl is not set! /// or /// SplunkHttpEventCollector Token is not set! /// </exception> protected override void InitializeTarget() { base.InitializeTarget(); if (string.IsNullOrEmpty(ServerUrl?.Authority)) { throw new NLogConfigurationException("SplunkHttpEventCollector ServerUrl is not set!"); } if (string.IsNullOrEmpty(Token)) { throw new NLogConfigurationException("SplunkHttpEventCollector Token is not set!"); } _metaData.Clear(); _hecSender = new HttpEventCollectorSender( ServerUrl, // Splunk HEC URL Token, // Splunk HEC token *GUID* GetMetaData(null), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order BatchSizeBytes == 0 && BatchSizeCount == 0 ? 0 : 250, // BatchInterval - Set to 0 to disable BatchSizeBytes, // BatchSizeBytes - Set to 0 to disable BatchSizeCount, // BatchSizeCount - Set to 0 to disable IgnoreSslErrors, new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin // Resend Middleware with retry ); _hecSender.OnError += (e) => { InternalLogger.Error(e, "SplunkHttpEventCollector(Name={0}): Failed to send LogEvents", Name); }; }
/// <summary> /// Initializes the target. Can be used by inheriting classes /// to initialize logging. /// </summary> /// <exception cref="NLogConfigurationException"> /// SplunkHttpEventCollector ServerUrl is not set! /// or /// SplunkHttpEventCollector Token is not set! /// </exception> protected override void InitializeTarget() { base.InitializeTarget(); NLog.Common.InternalLogger.Debug("Initializing SplunkHttpEventCollector"); _metaData.Clear(); var serverUri = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent()); if (string.IsNullOrEmpty(serverUri)) { throw new NLogConfigurationException("SplunkHttpEventCollector ServerUrl is not set!"); } var token = RenderLogEvent(Token, LogEventInfo.CreateNullEvent()); if (string.IsNullOrEmpty(token)) { throw new NLogConfigurationException("SplunkHttpEventCollector Token is not set!"); } var proxyConfig = UseProxy ? new ProxyConfiguration { ProxyUrl = RenderLogEvent(ProxyUrl, LogEventInfo.CreateNullEvent()), ProxyUser = RenderLogEvent(ProxyUser, LogEventInfo.CreateNullEvent()), ProxyPassword = RenderLogEvent(ProxyPassword, LogEventInfo.CreateNullEvent()) } : new ProxyConfiguration { UseProxy = false }; var channel = RenderLogEvent(Channel, LogEventInfo.CreateNullEvent()); var index = RenderLogEvent(Index, LogEventInfo.CreateNullEvent()); var source = RenderLogEvent(Source, LogEventInfo.CreateNullEvent()); var sourceType = RenderLogEvent(SourceType, LogEventInfo.CreateNullEvent()); _hecSender = new HttpEventCollectorSender( new Uri(serverUri), // Splunk HEC URL token, // Splunk HEC token *GUID* channel, // Splunk HEC data channel *GUID* GetMetaData(index, source, sourceType), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order BatchSizeBytes == 0 && BatchSizeCount == 0 ? 0 : 250, // BatchInterval - Set to 0 to disable BatchSizeBytes, // BatchSizeBytes - Set to 0 to disable BatchSizeCount, // BatchSizeCount - Set to 0 to disable IgnoreSslErrors, // Enable Ssl Error ignore for self singed certs *BOOL* proxyConfig, // Proxy Config - Set to false to disable MaxConnectionsPerServer, // MaxConnectionsPerServer new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin, // Resend Middleware with retry httpVersion10Hack: UseHttpVersion10Hack ); _hecSender.OnError += (e) => { InternalLogger.Error(e, "SplunkHttpEventCollector(Name={0}): Failed to send LogEvents", Name); }; }
/// <summary> /// Disposes the initialized HttpEventCollectorSender /// </summary> protected override void CloseTarget() { try { _hecSender?.Dispose(); base.CloseTarget(); } finally { _hecSender = null; } }
/// <summary> /// Initialize the options on the SplunkHttpEventCollector appender /// </summary> public override void ActivateOptions() { _hecSender = new HttpEventCollectorSender( new Uri(ServerUrl), // Splunk HEC URL Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(Index, null, "_json", GetMachineName()), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order 0, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable 0, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin // Resend Middleware with retry ); }
public override void ActivateOptions() { _hecSender = new HttpEventCollectorSender( new Uri(ServerUrl), Token, new HttpEventCollectorEventInfo.Metadata(Index, null, "_json", GetMachineName()), SendMode, 0, 0, 0, new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin, null, IgnoreCertificateErrors ); }
/// <summary> /// Initialize the options on the SplunkHttpEventCollector appender /// </summary> public override void ActivateOptions() { _hecSender = new HttpEventCollectorSender( new Uri(ServerUrl), // Splunk HEC URL Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(Index, null, "_json", GetMachineName()), // Metadata SendMode, BatchIntevalMs, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable BatchSizeCount, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin, // Resend Middleware with retry null, IgnoreCertificateErrors // Ignore invalid server certificate (self-signed, etc) ); }
static async void DoIt() { ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return(true); }; var middleware = new HttpEventCollectorResendMiddleware(100); var ecSender = new HttpEventCollectorSender(new Uri("https://localhost:8088"), "92A93306-354C-46A5-9790-055C688EB0C4", null, HttpEventCollectorSender.SendMode.Sequential, 5000, 0, 0, middleware.Plugin); ecSender.OnError += o => Console.WriteLine(o.Message); ecSender.Send(DateTime.UtcNow.AddDays(-1), Guid.NewGuid().ToString(), "INFO", null, new { Foo = "Bar", test2 = "Testit2", time = ConvertToEpoch(DateTime.UtcNow.AddHours(-2)).ToString(), anotherkey = "anothervalue" }); ecSender.Send(Guid.NewGuid().ToString(), "INFO", null, new { Foo = "Bar", test2 = "Testit2", time = ConvertToEpoch(DateTime.UtcNow.AddHours(-2)).ToString(), anotherkey = "anothervalue!!" }); await ecSender.FlushAsync(); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="filter"></param> /// <param name="configuration"></param> public SplunkLogger(string name, Func <string, LogLevel, Exception, bool> filter, SplunkConfiguration configuration) { Filter = filter ?? ((category, logLevel, exception) => true); this.configuration = configuration; this.name = name; hecSender = new HttpEventCollectorSender( configuration.ServerUrl, // Splunk HEC URL configuration.Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(null, null, "_json", GetMachineName()), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order 0, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable 0, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(configuration.RetriesOnError).Plugin // Resend Middleware with retry ); }
public static async Task LogDataToSplunk(string severity, string message, object data) { try { var middleware = new HttpEventCollectorResendMiddleware(100); var esSender = new HttpEventCollectorSender(new Uri("http://localhost:8088"), "73f4c508-35da-4d14-9d4b-12a8d820935f", null, HttpEventCollectorSender.SendMode.Sequential, 0, 0, 0, middleware.Plugin); esSender.OnError += EsSender_OnError; esSender.Send(Guid.NewGuid().ToString(), severity, message, data); var flushTask = esSender.FlushAsync(); flushTask.Start(); await flushTask; } catch (Exception ex) { } }
public SplunkAppender() { //Put connection string here ending with :8088 - Exclude tail of "/services/collector/event/1.0" as it will be appended _connectionString = new Uri("https://input-IAmYourEndPoint.cloud.splunk.com:8088"); _connectionToken = "I-R-SecretKey"; _middleware = new HttpEventCollectorResendMiddleware(100); _collectorSender = new HttpEventCollectorSender( _connectionString, _connectionToken, null, HttpEventCollectorSender.SendMode.Sequential, 0, 0, 0, _middleware.Plugin); _collectorSender.OnError += o => Trace.WriteLine(o.Message); }
public void ActivateOptions(HttpEventCollectorSender.HttpEventCollectorFormatter formatter) { _hecSender = new HttpEventCollectorSender( new Uri(ServerUrl), // Splunk HEC URL Token, // Splunk HEC token *GUID* new HttpEventCollectorEventInfo.Metadata(null, null, SourceType, GetMachineName()), // Metadata HttpEventCollectorSender.SendMode.Sequential, // Sequential sending to keep message in order 0, // BatchInterval - Set to 0 to disable 0, // BatchSizeBytes - Set to 0 to disable 0, // BatchSizeCount - Set to 0 to disable new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin, // Resend Middleware with retry formatter // Use a custom formatter before sending ); // throw error on send failure _hecSender.OnError += exception => { throw new Exception($"SplunkHttpEventCollector failed to send log event to Splunk server '{new Uri(ServerUrl).Authority}' using token '{Token}'. Exception: {exception}"); }; }
// Send Json objects directly to HTTP Event Collector private async Task <string> M3() { try { var middleware = new HttpEventCollectorResendMiddleware(100); var esSender = new HttpEventCollectorSender(new Uri("http://localhost:8088"), "73f4c508-35da-4d14-9d4b-12a8d820935f", null, HttpEventCollectorSender.SendMode.Sequential, 0, 0, 0, middleware.Plugin); esSender.OnError += EsSender_OnError; esSender.Send(Guid.NewGuid().ToString(), "INFO", "My Message", new Person() { FirstName = "Raj 1", LastName = "Konduri" }); var flushTask = esSender.FlushAsync(); flushTask.Start(); await flushTask; return("Success"); } catch (Exception ex) { return(ex.Message); } }