Something funny I encountered today
Today, while improving the error handling of an application at work, I came upon a rather interesting piece of (C#) code. The application is a Windows service used for scheduling “plugins” that process documents or generate them and whatnot. Anyway, if a fatal error has occurred during the execution of a plugin, the following bit of code is executed after logging the event to a text file:
// dt = timestamp of error
// id = origin of the error
// text = error message
// Always send email in following case
if (type == SchedulerInfoType.FatalError)
{
Mailer mailer = new Mailer();
try
{
mailer.SendEmail(ConfigurationSettings.AppSettings["BugEmailAddress"],
"scheduler@xxxxxx.xxx",
"A fatal error has occurred " + dt.ToString(),
String.Format(dt.ToString() + "-" + type.ToString() +
"-" + id + "-" + text ),
MailFormat.Html,
MailPriority.Normal );
}
catch(Exception ex)
{
mailer.SendEmail(ConfigurationSettings.AppSettings["BugEmailAddress"],
"scheduler@xxxxxx.xxx",
"An error occurred while sending the error email " + dt.ToString(),
String.Format(dt.ToString() + "-" + id + "-" + text + "\r\n") +
String.Format(ex.Message)
, MailFormat.Html,
MailPriority.Normal);
}
}
Also note that the use of String.Format is rather superfluous in this case as there’s nothing to format…