locked
Exception Handling suggestions RRS feed

  • Question

  • User-1104215994 posted

    Hi,

    In my web <g class="gr_ gr_23 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="23" data-gr-id="23">API</g> I would like to store exceptions. I can log exceptions to a file or insert into <g class="gr_ gr_129 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="129" data-gr-id="129">database</g>. I wonder which way would be <g class="gr_ gr_214 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="214" data-gr-id="214">more</g> appropriate or better solution? I would glad if you can guide me.

    Best Regards.

    Wednesday, February 20, 2019 8:41 AM

All replies

  • User753101303 posted

    Hi,

    IMO do things the other way round ie what do you want to do with those exceptions starting with who is responsible for checking them and possibly passing them back to the dev team (or you want alerts ?)

    If Windows log are closely monitored already you could perhaps send them there ?

    Wednesday, February 20, 2019 9:43 AM
  • User-1104215994 posted

    Hi <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="8" data-gr-id="8">PatriceSc</g>,

    <g class="gr_ gr_16 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="16" data-gr-id="16">Actually</g> I will responsible for checking exceptions since I am developing this web <g class="gr_ gr_217 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="217" data-gr-id="217">api</g> and there is no other guy :) I will insert requests/responses in <g class="gr_ gr_333 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="333" data-gr-id="333">database</g> so I think generating an exception table and inserting exceptions into it will be handy I think.

    Wednesday, February 20, 2019 11:56 AM
  • User475983607 posted

    Store exceptions in a database if you want to query the exceptions at a later time.  If you do not plan to query the exception then a file is fine.  

    Wednesday, February 20, 2019 12:32 PM
  • User-1104215994 posted

    For logging in a file, using log4net or <g class="gr_ gr_27 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="27" data-gr-id="27">elmah</g> is enough?

    Wednesday, February 20, 2019 1:14 PM
  • User475983607 posted

    For logging in a file, using log4net or elmah is enough?

    Only you can answer this question.  I assume either will work fine.

    Wednesday, February 20, 2019 1:21 PM
  • User753101303 posted

    Will you check the db on a regular basis ? It could be interesting to have maybe a daily summary. What if something really bad happens ? It could up to full dashboard with alerts such as https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview but it all depends on what YOU need.

    From an architectural point of view make sure to use a proper design such as https://stackoverflow.com/questions/46803813/can-i-use-iloggerfactory-in-asp-net-mvc-5-not-mvc-core-project so that you could easily change the destination for your logging information to something else when and as needed.

    Wednesday, February 20, 2019 1:49 PM
  • User-1104215994 posted

    Thank you <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">PatriceSc</g>. Even though some of my questions seem silly or very basic, your guidance is very important for me.

    Wednesday, February 20, 2019 2:07 PM
  • User-35771671 posted

    I wouldn't write code for this if I were you. There are a lot of both free and commercial products out there, that will help you store all exceptions happening in your API. The most known free one is ELMAH, which stores errors locally in a file, memory, database or <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="7" data-gr-id="7">what ever</g> output you prefer. Here is an ELMAH tutorial that I wrote to help you get started. Also, I'm the founder of the commercial product elmah.io, which help you store all exceptions in the cloud and much more. There are a lot of other options out there too, like Stackify and Raygun.

    Wednesday, February 20, 2019 4:26 PM
  • User1120430333 posted

    Hi,

    In my web API I would like to store exceptions. I can log exceptions to a file or insert into database. I wonder which way would be more appropriate or better solution? I would glad if you can guide me.

    Best Regards.

    Are you using a Global Exception Handler or do you have try/catches in the code?

    Wednesday, February 20, 2019 11:22 PM
  • User-1104215994 posted

    I am planning to use <g class="gr_ gr_33 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="33" data-gr-id="33">global</g> exception handler.

    Thursday, February 21, 2019 5:26 AM
  • User36583972 posted


    Hi cenk1536,

    In my web API I would like to store exceptions. I can log exceptions to a file or insert into database. I wonder which way would be more appropriate or better solution? I would glad if you can guide me.

    You need to choose your storage method according to the purpose of the log.

    The following links may give you some ideas.

    Logging and Exception handling, Versioning in ASP.NET WEB API
    https://www.codeproject.com/Articles/1250932/Logging-and-Exception-handling-Versioning-in-ASP-N

    Request logging and Exception handling/logging in Web APIs using Action Filters, Exception Filters and NLog
    https://www.codeproject.com/Articles/1028416/RESTful-Day-sharp-Request-logging-and-Exception-ha

    Best Regards,

    Yong Lu

    Thursday, February 21, 2019 7:05 AM
  • User1120430333 posted

    I am planning to use global exception handler.

    How? How are you going to do this? Are you using WebAPI 2.0 or WebAPI Core?

    Thursday, February 21, 2019 1:59 PM
  • User-1104215994 posted

    Web API

    Thursday, February 21, 2019 2:20 PM
  • User1120430333 posted

    Web API

    There is ASP.NET WebAPI 2.0 and there is ASP.NET WebAPI Core. Which one are you using? Do you know how to implement Global Exception Handling in whatever version of ASP.NET WebAPI project platform you are using?

    Thursday, February 21, 2019 4:49 PM
  • User-1104215994 posted

    Web API 2.0, I read some articles about global exception handling.

    Thursday, February 21, 2019 5:47 PM
  • User1120430333 posted

    Web API 2.0, I read some articles about global exception handling.

    Well, I guess you should be able to implement GHE. I was just checking to see if you knew how to implement GEH. Of course as you may or may not know,  you don't need a try/catch in any code in the WebAPI or in a classlib project that the WebAPI is referencing, becuase the GHE implemented in the WebAPI will catch all unhandled exceptions no matter where it is thrown. 

    Thursday, February 21, 2019 6:55 PM
  • User-2054057000 posted

    Use Filters to catch exception and log in to your Database.

    Reference: Exception Filters in MVC

    Sunday, February 24, 2019 1:01 PM