我尝试构建通用的 Logger 通知库,它可以以不同的方式登录 . 它可能包括文件记录,控制台应用程序日志记录,调试打印日志记录,数据库日志记录等 . 我希望有类似于类的东西,我可以在列表中定义不同的 Logger ,然后通过Add方法添加然后将该类传递给消费者类和火 DoLog()
从列表中提升 Logger . 目前我在 DoLog()
方法上堆叠 . 问题是不同的 Logger 类会有不同的Write()方法参数,我不知道到目前为止如何调用它们的不同参数方法 Write()
. 任何人都可以给出提示/想法如何解决或重建 . 除了任何提议欢迎 .
这是我目前的代码:
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: 我正在考虑使用像 ILogger<T>
这样的通用接口来创建我自己的Loggers但是目前我没有看到我可以使用那个T的地方,例如消息和路径是字符串 . 这可能是什么?