Errors... gotta catch them all

Sometimes during debugging you just want everything. So I’ve gone through the docs, and some various open source code examples of servicestack error handlers, and I’ve tried to make sure I’ve got a logger (I’m using serilog) to handle all the error cases.

This is from Configure.AppHost.cs and where I’ve expanded the Configure method to add my error handlers based on what I’ve seen other people doing. (Since there does appear to be some flexibility with where you can attach these, I went with the AppHost config.)

    public override void Configure(Container container)
    {
        Plugins.Add(new SharpPagesFeature());
        base.SetConfig(new HostConfig
        {
            UseSameSiteCookies = true,
            DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
        });
        this.ServiceExceptionHandlers.Add((httpReq, request, exception) =>
        {
            Serilog.Log.Error(exception, "ServiceStack ServiceExceptionHandlers Logger");
            return null;    
        });
        this.GatewayExceptionHandlers.Add((httpReq, request, exception) =>
        {
            Serilog.Log.Error(exception, "ServiceStack GatewayExceptionHandlers Logger");
        });
        this.UncaughtExceptionHandlers.Add((req, res, operationName, exception) =>
        {
            Serilog.Log.Error(exception, "ServiceStack UncaughtExceptionHandler Logger");
        });
    }

Have I set these up right? Do I also need the async ones? Are there any other handlers I should be adding to?

Any suggested improvements to this?

These are all the main Exception Handlers ServiceStack raises when executing requests, plugins like ServerEventsFeature will have their own OnError callback which are also logged to the configured Logging provider so you wouldn’t need to log them again.

There’s also OnStartupException() which are logged on Startup, although they’re already logged to the configured logging provider and are thrown on Startup in development (which prevents App from starting). If you set Config.StrictMode = true Exceptions on Startup will also throw in production release builds as well.

You don’t need to register Async handlers as they’re always both called.