I try to built generic logger notification library which could log in different way. It could include file logging, console application logging, debug print logging, database logging etc. I want to have something similar to class that i could define different loggers in list by adding then by Add method and then having that pass that class to consumer class and the fire DoLog()
to raise loggers from list. At the moment i stack on DoLog()
method. The problem is different loggers classes would have different parameters of their Write() method and i have no idea so far how to call their different parameters methods Write()
. Can anyone give a hint/idea how to resolve that or rebuild. Besides any proposition welcome.
This is my current code:
public interface ICanLog { }
public interface ICanLogConsole : ICanLog
{
void Write(string msg);
}
public interface ICanLogFile : ICanLog
{
void Write(string path, string mag);
}
public class ConsoleLogger : ICanLogConsole
{
public void Write(string msg)
{
throw new NotImplementedException();
}
}
public class FileLogger : ICanLogFile
{
public void Write(string path, string mag)
{
throw new NotImplementedException();
}
}
public class NotifictionEngine
{
public List<ICanLog> Loggers { get; }
public NotifictionEngine()
{
Loggers = new List<ICanLog>();
}
public void AddLoggers(ICanLog logger)
{
Loggers.Add(logger);
}
public void DoLog()
{
foreach (var logger in Loggers)
{
//logger should log in his specific way Write()
//how to call different parameters Write from loggers?
}
}
}
EDIT:
I was thinking about o use generic interface like ILogger<T>
to be able to create my own Loggers but at the moment i do not see where i could use that T as for instance message and path are strings. What could be T here?