Common.Logging - 如何获取日志写入回调?
我有一个库,它使用 Common.Logging。当发生对日志的任何写入时,我需要调用我的代码。
I have a library, which uses Common.Logging. I need to get my code called when any write to it's log occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想更改任何内容,您可以使用 AOP 拦截对 Common.Logging 的方法调用,但请记住,AOP 会增加大量开销,因为它在运行时在幕后注入代码。否则您可以包装/扩展Common.Loggin 并创建您自己的 Write 方法来调用委托,该委托将指向您想要执行的代码(一种回调)
If you don't want change anything you could use AOP to intercept the method call to the Common.Logging but bear in mind the AOP add lots of overhead as behind the scene it injects code at run time.Otherwise you could wrap/Extend the Common.Loggin and create your own Write method that invoke a delegate that will point at the code you want to be executed(kind of callback)
如果您的日志记录已设计为接口,则可以使用装饰器模式创建新的日志记录,该新的日志记录将在内部调用原始日志记录。
然后,您可以针对 ILogging(通过依赖注入)注册新的 DecoratorLogging,这样 ILogging 的任何使用者都不会知道他们实际上正在使用您的新记录器:)
if your logging has been designed to an interface, you can use Decorator pattern to create new Logging which will internally call the original Logging.
And then you can register your new DecoratorLogging against ILogging (via dependency injection) that you have so that any consumers of the ILogging will not know that they are actually using your new logger :)
将我自己的适配器设置为 LogManager.Adapter 工作正常。
Setting my own adapter into LogManager.Adapter worked ok.