DataDog / serilog-sinks-datadog-logs

Serilog Sink that sends log events to Datadog https://www.datadoghq.com/
Apache License 2.0
60 stars 42 forks source link

Add callback on error #51

Closed ogaca-dd closed 3 years ago

ogaca-dd commented 4 years ago

Add an error handler when an exception occurs.

static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .WriteTo.DatadogLogs(API_KEY, exceptionHandler: e =>
        {
            var cannotSendLogEventException = e as CannotSendLogEventException;
            if (cannotSendLogEventException != null)
            {
                Console.WriteLine($"ERROR: {cannotSendLogEventException}  LogEvents Count: {cannotSendLogEventException.LogEvents.Count()}");
            }
        })
        .CreateLogger();

    Log.Information("Test serilog");
    Log.CloseAndFlush();
}
ogaca-dd commented 4 years ago

@yuwaMSFT2,

Thanks a lot for the code review. I have updated the code based on your comments but there is a corner case I need to fix before pushing my commits.

ogaca-dd commented 4 years ago

@yuwaMSFT2,

I have updated the PR, let me know if it suits your needs.

yuwaMSFT2 commented 3 years ago

Thank you @ogaca-dd! Overall looks good to me!

IrisClasson commented 3 years ago

Would it be an idea to collect exceptions as an aggregated exception and pass that to the callback as a property on the custom exception? Seems like it would be useful to know why the logging failed.

owlyowl commented 3 years ago

Add an error handler when an exception occurs.

static void Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .WriteTo.DatadogLogs(API_KEY, exceptionHandler: e =>
        {
            var cannotSendLogEventException = e as CannotSendLogEventException;
            if (cannotSendLogEventException != null)
            {
                Console.WriteLine($"ERROR: {cannotSendLogEventException}  LogEvents Count: {cannotSendLogEventException.LogEvents.Count()}");
            }
        })
        .CreateLogger();

    Log.Information("Test serilog");
    Log.CloseAndFlush();
}

Is there a way in the exception handler to then get the logger for the current context and log a shorter exception? I was thinking in the case of payload being too large logging an error back in to datadog to let us know the caller.