I have some code to write Error Logs to different places like Console/SignalR Messages/Text File.
The code is as follows:-
public class Logger
{
public void WriteToLog(string message, LogType logType)
{
switch (logType)
{
case LogType.Console:
Console.WriteLine(message);
break;
case LogType.SignalR:
// Code to send message to SignalR Hub
break;
case LogType.File:
// Code to write in .txt file
break;
}
}
}
Where my LogType is a simple Enum as below:-
public enum LogType
{
Console,
SignalR,
File
}
But, when I think of Open-Close Principle, I am not getting optimal solution for it.
Edit:
I need to refactor my code as per open close principle. I need to optimize it, so that I don't need to Modify WriteToLog method once any LogType gets added.
WriteToLogmethod once anyLogTypegets added. \$\endgroup\$