Handle errors with Splunk logging for .NET

It can be difficult to diagnose connection problems in logging without seeing the exceptions that are actually thrown. The exceptions thrown during connection attempts and by the reconnection policy are available by adding a handler to listeners.

For HTTP Event Collector

Both HttpEventCollectorTraceListener and HttpEventCollectorSink have a method that takes an action to be executed on each exception thrown in the logging system:

public void AddLoggingFailureHandler(Action<Exception> handler)

For example, to have AddLoggingFailureHandler() write an error to the local console, you would write:

var myListener = new HttpEventCollectorTraceListener(
    uri: new Uri("https://splunk-server:8088"),
    token: "205A7CE0-24B6-44CD-9299-333E29BBBCF1");
myListener.AddLoggingFailureHandler((HttpEventCollectorException e) => {
    Console.WriteLine("{0}", ex);
});

For TCP inputs

Both TcpEventSink and TcpTraceListener have a method that takes an action to be executed on each exception thrown in the logging system:

public void AddLoggingFailureHandler(Action<Exception> handler)

For example, to have AddLoggingFailureHandler() write an error to the local console, you would write:

TcpTraceListener listener = ...;
listener.AddLoggingFailureHandler((ex) => {
    Console.WriteLine("{0}", ex);
});