public static void Log(string data, string host, string indexName, string userName, string password, int port = 8089, IErrorHandler errorHandler = null, bool useFreshSession = false, int sessionTimeout = 55) { try { indexName = indexName.ToLowerInvariant(); var service = CreateSplunkService(host, indexName, userName, password, port, useFreshSession, sessionTimeout); var receiver = service.GetReceiver(); var args = new ReceiverSubmitArgs() { Index = indexName, Source = "SplunkAppender", SourceType = "bigjson", Host = Environment.MachineName }; receiver.Submit(args, data); } catch (Exception ex) { if (errorHandler != null) { errorHandler.Error("Splunk Appender Log Exception.", ex); } } }
/// <summary> /// </summary> /// <param name="batchSizeLimit"></param> /// <param name="period"></param> /// <param name="connectionInfo"></param> public SplunkSink(int batchSizeLimit, TimeSpan period, ISplunkConnectionInfo connectionInfo) : base(batchSizeLimit, period) { _connectionInfo = connectionInfo; _service = new Service(connectionInfo.ServiceArgs); _receiveSubmitArgs = new ReceiverSubmitArgs { Source = _connectionInfo.SplunkSource, SourceType = _connectionInfo.SplunkEventType }; }
/// <summary> /// The main program /// </summary> /// <param name="argv">The command line arguments</param> public static void Main(string[] argv) { // Load connection info for Splunk server in .splunkrc file. var cli = Command.Splunk("submit"); cli.Parse(argv); var service = Service.Connect(cli.Opts); var args = new ReceiverSubmitArgs { Source = "splunk-sdk-tests", SourceType = "splunk-sdk-test-event", }; var receiver = new Receiver(service); // Submit to default index receiver.Submit(args, "Hello World."); receiver.Submit(args, "Goodbye world."); }
public void IndexArgs() { string indexName = "sdk-tests2"; DateTimeOffset offset = new DateTimeOffset(DateTime.Now); string now = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss") + string.Format("{0}{1} ", offset.Offset.Hours.ToString("D2"), offset.Offset.Minutes.ToString("D2")); Service service = this.Connect(); Index index = service.GetIndexes().Get(indexName); index.Enable(); Assert.IsFalse(index.IsDisabled); Args indexProperties = GetIndexProperties(index); ClearIndex(service, indexName, index); // submit event to index using variable arguments index.Submit(indexProperties, now + " Hello World. \u0150"); index.Submit(indexProperties, now + " Goodbye World. \u0150"); WaitUntilEventCount(index, 2, 45); ClearIndex(service, indexName, index); // stream event to index with variable arguments Stream streamArgs = index.Attach(indexProperties); streamArgs.Write(Encoding.UTF8.GetBytes(now + " Hello World again. \u0150\r\n")); streamArgs.Write(Encoding.UTF8.GetBytes(now + " Goodbye World again.\u0150\r\n")); streamArgs.Close(); WaitUntilEventCount(index, 2, 45); // submit event using ReceiverSubmitArgs const string Source = "splunk-sdk-tests"; const string SourceType = "splunk-sdk-test-event"; const string Host = "test-host"; var args = new ReceiverSubmitArgs { Index = indexName, Host = Host, Source = Source, SourceType = SourceType, }; var receiver = service.GetReceiver(); receiver.Submit(args, "Hello World."); receiver.Submit(args, "Goodbye world."); WaitUntilEventCount(index, 4, 45); // verify the fields of events in the index matching the args. using (var stream = service.Oneshot( string.Format( "search index={0} host={1} source={2} sourcetype={3}", indexName, Host, Source, SourceType))) using (var reader = new ResultsReaderXml(stream)) { Assert.AreEqual(2, reader.Count()); } ClearIndex(service, indexName, index); index.Clean(180); Assert.AreEqual(0, index.TotalEventCount, "Expected the total event count to be 0"); }