I would like to log unhandled exceptions. In my setup, I use OrmLite within a ServiceStack AppHost. Let’s say that I have a ConnectionString mismatch (illustrated below to easily demo what I mean). I will correctly receive an ArgumentNullException, but it does not make it to the log. The call itself gets logged, but not the exception.
I saw this link, andI tried mythz’s answer but it did not help:
Code follows. If you know of a way to easily enable logging of all unhandled exceptions, I’d appreciate hearing from you.
Startup.cs
public class Startup: ModularStartup
{
public Startup(IConfiguration configuration) : base(configuration) { }
public new void ConfigureServices(IServiceCollection services) { }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var appHost = new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration),
};
//I tried with and without this option
appHost.UncaughtExceptionHandlers.Add((req, res, operationName, ex) =>
{
FooService.Log.Error(ex);
});
app.UseServiceStack(appHost);
}
}
public class AppHost : AppHostBase
{
public AppHost() : base("Foo", typeof(ServiceInterface.FooService).Assembly) { }
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DefaultRedirectPath = "/metadata",
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false),
DefaultContentType = MimeTypes.Json,
EnableFeatures = Feature.All.Remove(Feature.Html),
});
//I've tried different combinations of these settings with no luck
OrmLiteConfig.ThrowOnError = true;
OrmLiteConfig.ExceptionFilter = (dbCmd, ex) =>
{
FooService.Log.Error(ex);
};
LogManager.LogFactory = new Log4NetFactory("log4net.config");
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(Configuration.GetConnectionString("Default"), SqlServer2016Dialect.Provider));
}
}
FooService.cs
public partial class FooService: Service
{
public static ILog Log = LogManager.GetLogger(typeof(FooService));
//https://localhost:44301/Bar?Baz=123456
public object Get(Bar request)
{
Log.Info(request.GetType().Name + ": " + request.Dump().CollapseWhitespace());
var q = Db.Single<Bar>(x => x.Baz == request.Baz); // How do I log that this line had an unhandled exception when running?
return "0";
}
appsettings.json (note the ConnectionString is Default2 here, but I’m looking for Default above in order to easily cause an exception and demo this)
{
"ConnectionStrings": {
"Default2": "Server=mssql; Database=db; user=sa; password=password"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"servicestack": {
"license": "mylicense"
},
"AllowedHosts": "*"
}
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="Logs/Logs.txt" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="DEBUG" />
</root>
</log4net>