Please check out my following code…
public enum LogType
{
Debug,
Info,
Warn,
Error,
Fatal
}
private static readonly ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void LogError(LogType logtype, string message)
{
XmlConfigurator.Configure();
if (logtype == LogType.Debug)
log.Debug(message);
else if (logtype == LogType.Error)
log.Error(message);
}
I do not like all the above if-else statements and believe that there is a cleaner way to write this. How could I refactor it? log class has different methods for Debug, Error etc. etc.
I would like to make a single call to a method have it automatically take care of it.
LogMyError(LogType.Debug, "I am just logging here");
How can I do such a thing? I prefer to stay away from switch statement. I am looking for a clean object oriented approach.
You can use a
Dictionary<LogType,Action<string>>to hold what action to perform for each enumeration value, then just call the delegate.Update:
This is all overkill if you do only have a small number of branches on your
ifstatements. I would use this method for 5+ such ifs.