title: TSQL: TRY CATCH error handling with logging errors into a table
body: <p>Using SQL Server 2014:</p>
  
  <p>I am going through the following article that includes useful patterns for TSQL error handling:</p>
  
  <p><a href="https://msdn.microsoft.com/en-IN/library/ms175976.aspx" rel="nofollow">https://msdn.microsoft.com/en-IN/library/ms175976.aspx</a></p>
  
  <p>I like to log errors so later on I can query, monitor, track and inspect the errors took place in my application's store procedures. </p>
  
  <p>I was thinking to create a table and insert the error details as a row into the table in the CATCH block; however I am concern this might not be a good pattern OR there might be a built-in SQL server feature that can log the errors generated by the ;THROW statement.</p>
  
  <p>What would be the best way to log the errors? </p>
  
  <p><strong>Update 1</strong></p>
  
  <p>I should mention that I always set XACT_ABORT on top of my SPs:</p>
  
  <p>SET XACT_ABORT, NOCOUNT ON</p>
  
  <p>Is it safe to assume that there is no way to log errors when XACT_ABORT is ON? </p>
  
  <p><strong>Update 2</strong></p>
  
  <p>The SET XACT_ABORT ON is according to this post:</p>
  
  <p><a href="http://www.sommarskog.se/error_handling/Part1.html#jumpXACT_ABORT" rel="nofollow">http://www.sommarskog.se/error_handling/Part1.html#jumpXACT_ABORT</a></p>
  
  <p>Can xp_logevent be a better alternative than adding an error record to a log table? </p>