Outputting Errors to User I currently am making a small application where a user will enter their ID which needs to be in a certain format.  When entered in the wrong format, different output errors are produced.

I currently output errors like this: 

public void errorLogger(string error)
        {
            errors += error + Environment.NewLine + "&lt;br&gt;";
        }


I have read that using += is not a great operator to use. Alternatively I have thought this may be better:

public void errorLogger(string key, string err)
        {
            if(!String.IsNullOrEmpty(err))
            output.Add(key, err);
        }


but then I basically do the same thing later:

    string keys = "";
    foreach (KeyValuePair&lt;string, string&gt; entry in core.errorLogger())
    {
        keys += entry.Key + ":       " + entry.Value + "&lt;br&gt;";
    }


Is there a cleaner way to output errors?  The output is injected under the submit button into:

errorSpan.InnerHtml = (keys);


This is what I ended up using going off of Charles' response to AWinkle

public void logError(string err, string desc)
                {
                    if(!String.IsNullOrEmpty(desc))
                        errors.AppendFormat("{0,10}:&amp;nbsp;{1}&lt;br&gt;", err, desc);
                }
public string getErrors()
            {
                return errors.ToString();
            }


Then :

errorspan.InnerHtml = (core.getErrors());
Using the StringBuilder class will typically be better for looped string building.

string keys = "";
foreach (KeyValuePair&lt;string, string&gt; entry in core.errorLogger())
{
    keys += entry.Key + ":       " + entry.Value + "&lt;br&gt;";
}


turns into

StringBuilder keys = new StringBuilder();
foreach (KeyValuePair&lt;string, string&gt; entry in core.errorLogger())
{
    keys.AppendFormat("{0}:&amp;nbsp;{1}&lt;br&gt;", entry.Key, entry.Value);
}


Then you take errorSpan.InnerHtml = (keys); and change it to errorSpan.InnerHtml = (keys.ToString());

Alternatively, you could use this to replace the dictionary as CharlesNRice suggests.

StringBuilder keys = new StringBuilder();

 public void errorLogger(string key, string err)
    {
        if(!String.IsNullOrEmpty(err))
        keys.AppendFormat("{0}:&amp;nbsp;{1}&lt;br&gt;", key,err);
    }