locked
Best way to implement global error handling? RRS feed

  • Question

  • What is the recommended/best way to implement a global error handler? There doesn't seem to be a global.asax where I can hook into an application-level error event, but maybe I can add one? Is there a better way?

    I built a table ErrorLog where I plan to keep a history of all unhandled exceptions, ideally including those that occur on the client.

    Regards,
    Roger Martin

    Tuesday, February 7, 2012 2:15 PM

Answers

  • It depends on exactly what you want to do.

    One thing you can do is turn on our tracing system.  The settings to do this are in web.config.  You should set this to "localonly=true" and choose the appropriate settings for sensitivity and verbosity. 

    If you'd really like some kind of entity based log, I would recommend the following

    1) put it in a separate data source from your main application tables.  that way it will have its own logic pipeline for reading/writing, and will not cause any transactional or other conflicts with your application stores

    2) a great place to start your server-side app specific logging would be in "SaveChanges_ExecuteFailed" on each of your application data services.  If this code gets called, it means a save has failed for some reason.  You can get information about the failure in this method and then write an entity to your logging data service.

    3) as long as the error log table is in a separate data source, you can call it from anywhere in your app without causing transactional problems.  However, when working from the client it is best to do all of the error log work in a new dataworkspace -- screens by default only allow for one writable datasource in a given dataworkspace.  So you'd want to do something like:

    dw = new DataworkSpace();

    errorLog = dw.ErrorlogDataService.ErrorLogs.AddNew();

    errorLog.Message = "foo";

    dw.ErrorlogDataService.SaveChanges();

    as a general pattern in your client code..

    Tuesday, February 7, 2012 5:19 PM

All replies

  • It depends on exactly what you want to do.

    One thing you can do is turn on our tracing system.  The settings to do this are in web.config.  You should set this to "localonly=true" and choose the appropriate settings for sensitivity and verbosity. 

    If you'd really like some kind of entity based log, I would recommend the following

    1) put it in a separate data source from your main application tables.  that way it will have its own logic pipeline for reading/writing, and will not cause any transactional or other conflicts with your application stores

    2) a great place to start your server-side app specific logging would be in "SaveChanges_ExecuteFailed" on each of your application data services.  If this code gets called, it means a save has failed for some reason.  You can get information about the failure in this method and then write an entity to your logging data service.

    3) as long as the error log table is in a separate data source, you can call it from anywhere in your app without causing transactional problems.  However, when working from the client it is best to do all of the error log work in a new dataworkspace -- screens by default only allow for one writable datasource in a given dataworkspace.  So you'd want to do something like:

    dw = new DataworkSpace();

    errorLog = dw.ErrorlogDataService.ErrorLogs.AddNew();

    errorLog.Message = "foo";

    dw.ErrorlogDataService.SaveChanges();

    as a general pattern in your client code..

    Tuesday, February 7, 2012 5:19 PM
  • Thanks for the reply. I want an error handling system that will run in production and write an entry to an error log any time an unhandled exception occurs. In ASP.NET, it is easy since all I have to do is hook into the error event in global.asax.

    It sounds like this is not possible in LS. Instead, I need to scatter try...catch statements at all entry points on the server and on the client. Yes, I can hook into SaveChanges_ExecuteFailed, but that will catch only a subset of errors.

    Is that right? Since LS (at least in my scenario) is running as an ASP.NET application, can I force the use of global.asax, specifically the Error event?

    Tuesday, February 7, 2012 7:24 PM
  • We haven't tried using global.asax, but in theory any server errors that aren't otherwise caught might show up there.

    Client errors I would not expect to be accessible that way.

    Tuesday, February 7, 2012 7:40 PM
  • Thanks. I'm not sure I want to be the first person to experiment with using glabal.asax in LS, so I may put off this part of my project for another day. I appreciate the feedback, though.

    Roger

    Tuesday, February 7, 2012 8:10 PM