Yve*_*lpe 4 c# logging log4net nlog
由于有很多日志框架,我希望为每个应用程序编写一个抽象,以便我控制可以插入的日志框架。这意味着我在例如 Log4Net 或 NLog 上不可靠,但是我提供了一个接口/抽象,然后在我自己的抽象中提供了插件框架。
沿着这些路线的东西(可能有点简化),我可以将其传递给任何需要依赖项的对象。
interface ILogger {
void LogMessage(string message, Severity severity);
void LogDebug(string message);
void LogInfo(string message);
void LogWarning(string message);
void LogError(string message);
void LogFatal(string message);
}
Run Code Online (Sandbox Code Playgroud)
之后我的计划是为我想要使用的日志框架编写适配器/实现 - 这样我就可以在必要时插入任何其他日志框架。
public sealed class Log4NetLogger : ILogger {
/// Implementation and Log4Net specifics here
}
public sealed class NlogLogger : ILogger {
/// Implementation and Log4Net specifics here
}
Run Code Online (Sandbox Code Playgroud)
这些我将通过 IoC 或资源定位器注入/实例化,具体取决于我正在处理的应用程序的实现细节。
问题:我不知道这是否是个好主意?从头开始写这个,考虑可能
另一方面,对我的好处是:
任何指导、评论、警告应该和不应该 - 或者对现有抽象的引用可能很方便并且非常受欢迎!
谢谢,伊夫
好吧,在我的热情中,我似乎瞥了一眼。我可能已经找到了一个适合我需要的库,如果有人正在寻找类似的解决方案,我会在这里发布它:Common Logging framework。
是什么? 提供一个简单的日志抽象来在不同的日志实现之间切换。当前支持 log4net、NLog、Microsoft Enterprise Library 日志记录、Microsoft Application Insights、Microsoft Event Tracing for Windows 和 Serilog。此外,Common.Logging 带有一组基类,使任何日志系统的集成变得轻而易举。~ 取自他们的官方 github repo。
参考资料:
| 归档时间: |
|
| 查看次数: |
914 次 |
| 最近记录: |