I have the following method that's in a common library. I want to know how to write it better, but mostly, I want to know how to think about this better so I don't end up with code like this.
public virtual void PrintConsoleAndLog(string verboseMessage = null, Exception e = null,
OnErrorEventsArgs.ShowExceptionLevel exceptionLevel =
OnErrorEventsArgs.ShowExceptionLevel.Error)
{
switch (exceptionLevel)
{
case OnErrorEventsArgs.ShowExceptionLevel.Info:
if (e == null)
{
Logger.Info("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
Console.WriteLine("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
}
else
{
if (verboseMessage == null)
{
Logger.Info(String.Format("\n[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace);
}
else
{
Logger.Info(String.Format("\n[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace);
}
}
break;
case OnErrorEventsArgs.ShowExceptionLevel.Debug:
if (e == null)
{
Logger.Debug("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
Console.WriteLine("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
}
else
{
if (verboseMessage == null)
{
Logger.Debug(String.Format("\n[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace);
}
else
{
Logger.Debug(String.Format("\n[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace);
}
}
break;
// note that OnErrorEventsArgs.ShowExceptionLevel.Error will end up here also
default:
if (e == null)
{
Logger.Error("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
Console.WriteLine("\n[{0}] {1}", DateTime.Now.ToShortTimeString(), verboseMessage);
}
else
{
if (verboseMessage == null)
{
Logger.Error(String.Format("\n[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}]Base Exception: {1}\n\nStacktrace: {2}",
DateTime.Now.ToShortTimeString(),
e.GetBaseException(), e.StackTrace);
}
else
{
Logger.Error(String.Format("\n[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace));
Console.WriteLine("[{0}] Error: {1}\n\nBase Exception: {2}\n\nStacktrace: {3}",
DateTime.Now.ToShortTimeString(), verboseMessage,
e.GetBaseException(), e.StackTrace);
}
}
break;
}
}