HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

How do I improve this logging mechanism?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisloggingimprovemechanismhow

Problem

There are quite a few things I am considering:

  • I will have to check for null values



  • 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
                                   {
                                       {"Inner Exception", e.InnerException.Message},
                                       {"Message", e.Message},
                                       {"Source", e.Source},
                                       {"StackTrace", e.StackTrace},
                                       {"MethodName",e.TargetSite.Name}
                                   };
            foreach (KeyValuePair 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();

Solution

Why would you have serious trouble with persistance?

Just do null checks for InnerException and TargetSite, rest will be inserted as null or string empty depending on your db setup.

And why not just simplify it as:

var innerExceptionMessage = e.InnerException == null ? string.Empty : e.InnerException.Message;
var targetSiteMessage     = e.TargetSite == null ? string.Empty : e.TargetSite.Message;

exceptionLog.Add("Inner Exception", innerExceptionMessage);
exceptionLog.Add("Message", e.Message);
exceptionLog.Add("Source", e.Source);
exceptionLog.Add("StackTrace", e.StackTrace);
exceptionLog.Add("MethodName",targetSiteMessage);


Keep it simple :)

Code Snippets

var innerExceptionMessage = e.InnerException == null ? string.Empty : e.InnerException.Message;
var targetSiteMessage     = e.TargetSite == null ? string.Empty : e.TargetSite.Message;

exceptionLog.Add("Inner Exception", innerExceptionMessage);
exceptionLog.Add("Message", e.Message);
exceptionLog.Add("Source", e.Source);
exceptionLog.Add("StackTrace", e.StackTrace);
exceptionLog.Add("MethodName",targetSiteMessage);

Context

StackExchange Code Review Q#5682, answer score: 5

Revisions (0)

No revisions yet.