Skip to main content
deleted 3 characters in body; edited tags; edited title
Source Link
palacsint
  • 29.9k
  • 9
  • 81
  • 156

How do iI improve this logging mechanism?

There are quite a few things iI am considering:

  1. I will have to check for null values

    I will have to check for null values
  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }

    I will have serious trouble persisting it in the database.

How do I improve this or re-factor this for better results?

public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var exceptionLog = new Dictionary<String, String>
                                   {
                                       {"Inner Exception", e.InnerException.Message},
                                       {"Message", e.Message},
                                       {"Source", e.Source},
                                       {"StackTrace", e.StackTrace},
                                       {"MethodName",e.TargetSite.Name}
                                   };
            foreach (KeyValuePair<String, String> kvp in e.Data)
                exceptionLog.Add(kvp.Key, kvp.Value);
        }
    }

Now what if there is an error in updating this log  , how will iI log that  ?

How do i improve this logging mechanism?

There are quite a few things i am considering

  1. I will have to check for null values

  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }

Now what if there is an error in updating this log  , how will i log that  ?

How do I improve this logging mechanism?

There are quite a few things I am considering:

  1. I will have to check for null values
  2. I will have serious trouble persisting it in the database.

How do I improve this or re-factor this for better results?

public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var exceptionLog = new Dictionary<String, String>
                                   {
                                       {"Inner Exception", e.InnerException.Message},
                                       {"Message", e.Message},
                                       {"Source", e.Source},
                                       {"StackTrace", e.StackTrace},
                                       {"MethodName",e.TargetSite.Name}
                                   };
            foreach (KeyValuePair<String, String> kvp in e.Data)
                exceptionLog.Add(kvp.Key, kvp.Value);
        }
    }

Now what if there is an error in updating this log, how will I log that?

Tweeted twitter.com/#!/StackCodeReview/status/130287831389184000
added 1382 characters in body
Source Link
ashutosh raina
  • 601
  • 1
  • 7
  • 18

There are quite a few things i am considering

  1. I will have to check for null values

  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }

Update with the new and simpler class

  public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var innerExcpetionMessage = e.InnerException == null ? "Null" : e.InnerException.Message;
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {                
                    var cmd = new SqlCommand("ApplicationErrorLog", connection) { CommandType = System.Data.CommandType.StoredProcedure };
                    cmd.Parameters.AddWithValue("@Type", e.GetType().Name);
                    cmd.Parameters.AddWithValue("@InnerException", innerExcpetionMessage);
                    cmd.Parameters.AddWithValue("@Message", e.Message);
                    cmd.Parameters.AddWithValue("@Source", e.Source);
                    cmd.Parameters.AddWithValue("@StackTrace", e.StackTrace);
                    cmd.Parameters.AddWithValue("@MethodName", e.TargetSite.Name);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }
                
            }
        }
    }

Now what if there is an error in updating this log , how will i log that ?

There are quite a few things i am considering

  1. I will have to check for null values

  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }

There are quite a few things i am considering

  1. I will have to check for null values

  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }

Update with the new and simpler class

  public class ErrorLog
    {
        public static void LogError(Exception e)
        {
            var innerExcpetionMessage = e.InnerException == null ? "Null" : e.InnerException.Message;
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {                
                    var cmd = new SqlCommand("ApplicationErrorLog", connection) { CommandType = System.Data.CommandType.StoredProcedure };
                    cmd.Parameters.AddWithValue("@Type", e.GetType().Name);
                    cmd.Parameters.AddWithValue("@InnerException", innerExcpetionMessage);
                    cmd.Parameters.AddWithValue("@Message", e.Message);
                    cmd.Parameters.AddWithValue("@Source", e.Source);
                    cmd.Parameters.AddWithValue("@StackTrace", e.StackTrace);
                    cmd.Parameters.AddWithValue("@MethodName", e.TargetSite.Name);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }
                
            }
        }
    }

Now what if there is an error in updating this log , how will i log that ?

Source Link
ashutosh raina
  • 601
  • 1
  • 7
  • 18

How do i improve this logging mechanism?

There are quite a few things i am considering

  1. I will have to check for null values

  2. I will have serious trouble persisting it in the database. How do i improve this or re-factor this for better results ?

    public class ErrorLog { public static void LogError(Exception e) { var exceptionLog = new Dictionary<String, String> { {"Inner Exception", e.InnerException.Message}, {"Message", e.Message}, {"Source", e.Source}, {"StackTrace", e.StackTrace}, {"MethodName",e.TargetSite.Name} }; foreach (KeyValuePair<String, String> kvp in e.Data) exceptionLog.Add(kvp.Key, kvp.Value); } }