What is the best way to log and accumulate exceptions from different servers?

Viewed 235

Currently, we have a few instances of WebApi applications on different physical servers. Sometimes, some exceptions happen and reduce the quality of service. As the exceptions log on servers themselves, and we need to check them individually, it will take a while to be aware of the problem.

For collecting all exceptions, I considered Exception Filters, but we have some try/catch without rethrowing exceptions, so we lose some exception. I also considered FirstChanceException Event, to log all exceptions at the first chance. For accumulating exceptions in one place, I considered Nlog, and use a shared folder. Are they good approaches? Is there a better approach or an open-source library for these?

I would like to accumulate all exceptions (whatever they handled or not) from all servers to a single place to check and process quickly.

1 Answers

Thanks to Panagiotis Kanavos, I've finally come up with this solution.

I took advantage of AppDomain.FirstChanceException Event to find all exceptions in my app.

static void Main()
{
    AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler;
}

static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e)
{

}

Then send them to InfluxDB, which is an open-source time-series database.

var payload = new LineProtocolPayload();
const string measurement = "logs";
var memoryLoad = new LineProtocolPoint(measurement,
     new Dictionary<string, object>
     {
         {"message", e.Exception?.Message,},
     },
     new Dictionary<string, string>
     {
         {"host", Environment.GetEnvironmentVariable("COMPUTERNAME")},
         {"app", "{my app name}"},
     },
     DateTime.UtcNow);
payload.Add(memoryLoad);       
var influxResult = Client.WriteAsync(payload, ct).Result;

And finally, I showed the result in Grafana, and this is the final result.

enter image description here

Related