private static void InitializeTraceSource() { if (traceSource == null) { if (_splunkConnectionString == null) { throw new Exception("SplunkLogService connection string not supplied"); } if (_splunkToken == null) { throw new Exception("SplunkLogService token not supplied"); } traceSource = new TraceSource($"{_projectName}Logger"); traceSource.Switch.Level = SourceLevels.All; traceSource.Listeners.Clear(); listener = new HttpEventCollectorTraceListener( uri: _splunkConnectionString, token: _splunkToken.ToString().ToUpper(), sendMode: HttpEventCollectorSender.SendMode.Parallel, batchInterval: 1000, batchSizeBytes: 10240, batchSizeCount: 10 ); listener.AddLoggingFailureHandler(e => { //if there are issues writing to splunk they should show up here. Trace.TraceError(e.Message); }); traceSource.Listeners.Add(listener); } }
private static void TraceListenerExample() { // Replace with your HEC token string token = "1ff51387-405a-4566-9f6a-87bc3fb44424"; // TraceListener var trace = new TraceSource("demo-logger"); trace.Switch.Level = SourceLevels.All; var listener = new HttpEventCollectorTraceListener( uri: new Uri("https://127.0.0.1:8088"), token: token, batchSizeCount: 1); trace.Listeners.Add(listener); // Send some events trace.TraceEvent(TraceEventType.Error, 0, "hello world 0"); trace.TraceEvent(TraceEventType.Information, 1, "hello world 1"); trace.TraceData(TraceEventType.Information, 2, "hello world 2"); trace.TraceData(TraceEventType.Error, 3, "hello world 3"); trace.TraceData(TraceEventType.Information, 4, "hello world 4"); trace.Close(); // Now search splunk index that used by your HEC token you should see above 5 events are indexed }