I am working with Azure mobile app table controllers. get syntax is
public IQueryable<Employee> GetAllEmployee()
{
try
{
return Query();
}
catch(Exception ex)
{
throw;
}
}
Now the issue here is, since the return method is IQueryable, i am not able to catch exceptions in the catch block, i understand IQueryable is for different requests from client (in my case android). however i want to log errors in the catch block.currently my debugger never lands in catch block . because azure mobile app sdk handles exception and forms http exceptions and all i can see is 500 exception. i want to log errors in database , how can i achieve this?
As you says the return type is IQueryable, so you couldn't catch the exception in the the GetAllEmployee method.
Here is a work around.
I suggest you could use web api global error handling to handle the exception. More details, you could refer to this article and below codes.
In the Startup.MobileApp.cs:
Add this class:
public class TraceSourceExceptionLogger : ExceptionLogger
{
private readonly TraceSource _traceSource;
public TraceSourceExceptionLogger(TraceSource traceSource)
{
_traceSource = traceSource;
}
public override void Log(ExceptionLoggerContext context)
{
//in this method get the exception details and add it to the sql databse
_traceSource.TraceEvent(TraceEventType.Error, 1,
"Unhandled exception processing {0} for {1}: {2}",
context.Request.Method,
context.Request.RequestUri,
context.Exception);
}
}
Change the ConfigureMobileApp method as below:
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.Services.Add(typeof(IExceptionLogger),
new TraceSourceExceptionLogger(new
TraceSource("MyTraceSource", SourceLevels.All)));
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
// Use Entity Framework Code First to create database tables based on your DbContext
Database.SetInitializer(new MobileServiceInitializer());
MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
SigningKey = ConfigurationManager.AppSettings["SigningKey"],
ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
TokenHandler = config.GetAppServiceTokenHandler()
});
}
app.UseWebApi(config);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.