title: .NET - log exceptions to db using multiple SqlParameters and specify what goes in each db column
body: <p>I'm following <code>KudVenkat</code>'s tutorial for logging exceptions to the database in <code>.NET</code>: <a href="http://csharp-video-tutorials.blogspot.com/2012/12/logging-exception-to-database-part-75.html" rel="nofollow noreferrer">http://csharp-video-tutorials.blogspot.com/2012/12/logging-exception-to-database-part-75.html</a></p>
  
  <p>His way is good for storing one thing - an ExceptionMessage string containing Exception Message, Exception Type, Stack Trace, etc - into one column - the <code>@exceptionMessage</code> column.</p>
  
  <p>I'm trying to split this full Exception Message up into parts, such as ExceptionType (to the @exceptionType column), exceptionMessage (to the @exceptionMessage column), StackTrace (to the @stackTrace column), etc.</p>
  
  <p>So far, I'm able to pass in multiple <code>SqlParameters</code> using:</p>
  
  <pre><code>        List&lt;SqlParameter&gt; sp = new List&lt;SqlParameter&gt;() {
              new SqlParameter("@exceptionType", log) { },
              new SqlParameter("@exceptionMessage", log) { }
  
          };
  
          cmd.Parameters.AddRange(sp.ToArray());
  </code></pre>
  
  <p>But I can't seem to split this data out into the columns I desire - the way I'm doing it, I have to call my logger function for each of my SqlParameters <strong>(see my code comments below)</strong>, and they each create a new row with the same message on every column. Rather than <code>Type</code> going to Type column and <code>Message</code> going to Message column, it creates one row with <code>Message</code> inserted into both the Type and Message columns, then another row with <code>Type</code> inserted into both the Type and Message columns.</p>
  
  <p>I believe this has to do with the nature of my LogToDB class:</p>
  
  <pre><code>public class storedProcedure : ApiController
  {
      public static void Log(Exception exception)
      {
  
          // exception type
          StringBuilder sbExceptionType = new StringBuilder();
  
          // exception message
          StringBuilder sbExceptionMessage = new StringBuilder();
  
          do
          {
              // exception type
              sbExceptionType.Append("Exception Type" + Environment.NewLine);
              sbExceptionType.Append(exception.GetType().Name);
              sbExceptionType.Append(Environment.NewLine + Environment.NewLine);
  
              // exception message
              sbExceptionMessage.Append("Message" + Environment.NewLine);
              sbExceptionMessage.Append(exception.Message + Environment.NewLine + Environment.NewLine);
              sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
              sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);
  
              exception = exception.InnerException;
          }
          while (exception != null);
  
  ////Calling this twice is a problem. How can I rebuild this to log both into the appropriate column?////
          LogToDB(sbExceptionMessage.ToString());
          LogToDB(sbExceptionType.ToString());
      }
  
      private static void LogToDB(string log)
      {
  
          string connectionString = ConfigurationManager.ConnectionStrings["redacted"].ConnectionString;
  
          using (SqlConnection con = new SqlConnection(connectionString))
          {
              SqlCommand cmd = new SqlCommand("redacted", con);
  
              cmd.CommandType = CommandType.StoredProcedure;
  
              List&lt;SqlParameter&gt; sp = new List&lt;SqlParameter&gt;() {
                  new SqlParameter("@exceptionType", log) { },
                  new SqlParameter("@exceptionMessage", log) { }
  
              };
  
              cmd.Parameters.AddRange(sp.ToArray());
  
              con.Open();
              cmd.ExecuteNonQuery();
              con.Close();
          }
      }
  }
  </code></pre>