catch exception of serilog sink

119 views Asked by At

Im trying to catch an exception in an asp.net core app which seems to be not directly on application level. Im using serilog with Serilog.AspNetCore and an mqtt-sink:

  builder.Host.UseSerilog((ctx, lc) => lc
        .WriteTo.Console()
        .WriteTo.MQTT(publisherOptions, "top/log", formatter: new RenderedCompactJsonFormatter()));

If the mqtt broker is not reachable at one point the mqtt-sink throws an Exception when logging. How could I catch this exception for handling? Since Serilog.AspNetCore is routing the asp.net logs I cannot just wrap my own logs with try and catch. Also the mqtt-sink is an external library. I tried to wrap the app.Run() command with try and catch but it is not working. Has anyone an idea what needs to be done? Edit: The mqtt-sink library implements the emit function in the following way:

public async void Emit(LogEvent logEvent)
        {            
            var stringWriter = new StringWriter();
            Formatter.Format(logEvent, stringWriter);

            var applicationMessage = ... //shortened
            
            await MqttClient.PublishAsync(applicationMessage);
        }

The awaited call to MqttClient.Async throws the exception.

0

There are 0 answers