2017-05-30 1 views
0

私はAzureモバイルアプリテーブルコントローラを使用しています。構文はAzureモバイルアプリテーブルコントローラの例外処理

public IQueryable<Employee> GetAllEmployee() 
    { 
     try 
     { 
     return Query(); 
     } 
     catch(Exception ex) 
     { 
      throw; 
     } 
    } 

今すぐ復帰方法がのIQueryableであるため、ここでの問題は、ですが、私はcatchブロックで例外をキャッチすることはできませんよされます、私は(私の場合のアンドロイドに)のIQueryableは、クライアントからの異なる要求のためであることを理解します。しかし、私はcatchブロックにエラーを記録したいと思っています。現在、私のデバッガは決してcatchブロックに入らない。紺色のモバイルアプリケーションのSDHKは例外を処理し、HTTP例外を形成するので、私は500の例外を見ることができます。私はデータベースにエラーをログに記録したい、どのように私はこれを達成することができますか?

答えて

1

戻り値の型はIQueryableなので、GetAllEmployeeメソッドで例外をキャッチできませんでした。

これは回避策です。

例外を処理するには、web api global error handlingを使用することをお勧めします。詳細については、article以下のコードを参照してください。 Startup.MobileApp.csで

は、このクラスを追加します。以下のように

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); 
     } 
    } 

変更ConfigureMobileApp方法:

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); 
     } 
+0

iがfor.thankあなたを探していたまさに! –