1

I'm following KudVenkat's tutorial for logging exceptions to the database in .NET: http://csharp-video-tutorials.blogspot.com/2012/12/logging-exception-to-database-part-75.html

His way is good for storing one thing - an ExceptionMessage string containing Exception Message, Exception Type, Stack Trace, etc - into one column - the @exceptionMessage column.

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.

So far, I'm able to pass in multiple SqlParameters using:

        List<SqlParameter> sp = new List<SqlParameter>() {
            new SqlParameter("@exceptionType", log) { },
            new SqlParameter("@exceptionMessage", log) { }

        };

        cmd.Parameters.AddRange(sp.ToArray());

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 (see my code comments below), and they each create a new row with the same message on every column. Rather than Type going to Type column and Message going to Message column, it creates one row with Message inserted into both the Type and Message columns, then another row with Type inserted into both the Type and Message columns.

I believe this has to do with the nature of my LogToDB class:

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<SqlParameter> sp = new List<SqlParameter>() {
                new SqlParameter("@exceptionType", log) { },
                new SqlParameter("@exceptionMessage", log) { }

            };

            cmd.Parameters.AddRange(sp.ToArray());

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
2
  • 1
    yes, you're passing the same value to both parameters. log - change LogToDB(string log) to LogToDB(string exceptionMessage, string exceptionType) and pass the values accordingly... Commented Jul 16, 2018 at 21:12
  • You are correct - thanks a lot! Commented Jul 16, 2018 at 21:33

1 Answer 1

2

Calling LogToDb twice causes inserting 2 rows into the database.

Passing log as @exceptionType and @exceptionMessage parameter values causes same value to be inserted in Type and Message columns.

Modify LogToDB as

private static void LogToDB(string exceptionType, string exceptionMessage)
{
   ....
   List<SqlParameter> sp = new List<SqlParameter>() {
            new SqlParameter("@exceptionType", exceptionType) { },
            new SqlParameter("@exceptionMessage", exceptionMessage) { }

        };
   ...

 }

And invoke LogToDB only once (Instead of twice as you do now)

LogToDB(sbExceptionType.ToString(), sbExceptionMessage.ToString());
Sign up to request clarification or add additional context in comments.

1 Comment

I've never had a quicker reply or a more direct fix - thank you very much, this worked. Glad the fix was relatively simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.