复合应用程序的最佳日志记录方法?
我正在创建一个包含多个不同项目(Shell、模块等)的复合 WPF (Prism) 应用程序。我正准备使用 Log4Net 实现日志记录。似乎有两种方法可以设置日志记录:
让 Shell 项目执行所有实际日志记录。它获取对 Log4Net 的引用,其他项目会触发复合事件,让 Shell 知道它需要记录某些内容。这些项目仅在 Shell 的 app.config 文件中打开日志记录的级别(DEBUG、ERROR 等)触发事件,以免降低性能。
为每个项目(包括模块)提供一个 Log4Net 参考,并让项目将自己的日志记录到一个公共日志文件中,而不是将消息发送到 Shell 进行日志记录。
哪种方法更好?或者,我应该考虑另一种方法吗?感谢您的帮助。
I am creating a Composite WPF (Prism) app with several different projects (Shell, modules, and so on). I am getting ready to implement logging, using Log4Net. It seems there are two ways to set up the logging:
Let the Shell project do all of the actual logging. It gets the reference to Log4Net, and other projects fire composite events to let the Shell know that it needs to log something. Those projects fire the events only for levels where logging is turned on in the Shell's app.config file (DEBUG, ERROR, etc), so as not to degrade performance.
Give each project, including modules, a Log4Net reference, and let the project do its own logging to a common log file, instead of sending messages to the Shell for logging.
Which is the better approach? Or, is there another approach that I should consider? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
登录 Prism 的最简单方法是覆盖 Bootstrapper 中的
LoggerFacade
属性。通过重写 LoggerFacade,您可以传入任何您想要的 Logger 实例以及所需的任何配置,只要该 Logger 实现了 ILoggerFacade 接口即可。我发现以下内容对于日志记录非常有效(我正在使用 Enterprise Libary Logging 块,但对 Log4Net 应用类似的内容应该很简单):
在 Shell 中创建 Boostrapper:
在基础设施项目中创建日志适配器,即:
MyCustomLoggerAdapter 类将用于覆盖 Bootstrapper 中的“LoggerFacade”属性。它应该有一个默认的构造函数来更新所有内容。
注意:通过覆盖 Bootstrapper 中的
LoggerFacade
属性,您可以为 Prism 提供一个日志记录机制,用于记录其自己的内部消息。您可以在整个应用程序中使用此记录器,也可以扩展该记录器以获得功能更齐全的记录器。 (请参阅MyCustomLoggerAdapterExtendedAdapter
/IFormalLogger
)MyCustomLoggerAdapterExtendedAdapter 派生自 MyCustomLoggerAdapter,并且可以提供额外的构造函数以实现更完整的功能。 -成熟的记录器。
在 Bootstrapper 中:
最后,要使用任一记录器,您所需要做的就是将适当的接口添加到类的构造函数中,UnityContainer 将为您注入记录器:
我认为您不需要用于日志记录策略的单独模块。通过将日志记录策略添加到基础设施模块,所有其他模块都将获得所需的引用(假设您将基础设施模块添加为对其他模块的引用)。通过将记录器添加到 Boostrapper,您可以让 UnityContainer 根据需要注入日志记录策略。
CodePlex 上的 CompositeWPF contrib 项目上有一个 uisng Log4Net 的简单示例以及。
HTH 的
The simplest approach to logging in Prism is to override the
LoggerFacade
property in your Bootstrapper. By overridding theLoggerFacade
, you can pass in an instance of any Logger you want with any configuration needed as long as the logger implements theILoggerFacade
interface.I've found the following to work quite well for logging (I'm using the Enterprise Libary Logging block, but applying something similar for Log4Net should be straight forward):
Create a Boostrapper in your Shell:
Create a Logging Adapter in your Infrastructure project, i.e.:
The MyCustomLoggerAdapter class will be used to override the 'LoggerFacade' property in the Bootstrapper. It should have a default contstructor that news everything up.
Note: by overriding the
LoggerFacade
property in the Bootstrapper, you are providing a logging mechanisim for Prism to use to log its own internal messages. You can use this logger throughout your application, or you can extend the logger for a more fully featured logger. (seeMyCustomLoggerAdapterExtendedAdapter
/IFormalLogger
)The MyCustomLoggerAdapterExtendedAdapter is dervied from the MyCustomLoggerAdapter and can provide additional constructors for a more full-fledged logger.
In the Bootstrapper:
Finally, to use either logger, all you need to do is add the appropriate interface to your class' constructor and the UnityContainer will inject the logger for you:
I don't think you need a separate module for the logging policy. By adding the logging policies to your infrastructure module, all other modules will get the required references (assuming you add the infrastructure module as a reference to your other modules). And by adding the logger to your Boostrapper, you can let the UnityContainer inject the logging policy as needed.
There is a simple example of uisng Log4Net on the CompositeWPF contrib project on CodePlex as well.
HTH's
我终于回到了这个问题,结果发现答案真的很简单。在 Shell 项目中,将 Log4Net 配置为自定义记录器。 Prism 文档(2009 年 2 月)第 12 页解释了如何执行此操作。 287)。 Shell 项目是唯一需要引用 Log4Net 的项目。要访问记录器(假设所有模块都传递了对 Prism IOC 容器的引用),只需解析 IOC 容器中的 ILoggerFacade,这将为您提供对自定义记录器的引用。以正常方式向此记录器传递消息。
因此,不需要将任何事件返回到 Shell,也不需要模块具有 Log4Net 引用。天哪,我喜欢 IOC 容器!
I finally got back to this one, and it turns out the answer is really pretty simple. In the Shell project, configure Log4Net as a custom logger. The Prism Documentation (Feb. 2009) explains how to do that at p. 287). The Shell project is the only project that needs a reference to Log4Net. To access the logger (assuming all modules are passed a reference to the Prism IOC container), simply resolve ILoggerFacade in the IOC container, which will give you a reference to your custom logger. Pass a message to this logger in the normal manner.
So, there is no need for any eventing back to the Shell, and no need for modules to have Log4Net references. Holy mackerel, I love IOC containers!
上面建议的
LoggerFacade
的问题是应用程序的非棱镜部分不会知道它。恕我直言,记录器需要比复合框架内的更低级别和更普遍的可访问性。我的建议是,为什么不只依赖标准的
Debug/Trace
并实现您自己的TraceListener
。这样,它对于 Prism/nonPrism 部件都适用。您可以通过此实现所需的灵活性。The problem with
LoggerFacade
, suggested above, is that the non prism parts of your app wouldn't know about it. Logger IMHO needs to be more low level and more universally accessible than just within the Composite framework.My suggestion is, why not just rely on standard
Debug/Trace
and implement your ownTraceListener
. This way it will work well for both Prism/nonPrism parts. You can achieve desired level of flexibility with this.为每个模块设置单独的记录器配置可能会在部署时出现问题。请记住,高级用户或管理员可能会完全更改您的日志记录目标,重定向到数据库或中央存储库聚合日志记录服务(例如 我公司的一个)。如果所有单独的模块都有单独的配置,则高级用户/管理员必须为每个模块重复配置(在每个 .config 文件中,或在主 app.config 中的每个模块部分中),并在每次位置更改时重复此操作/发生格式化。此外,考虑到附加程序是在运行时从配置中添加的,并且可能存在您目前不了解的附加程序,有人可能会使用锁定文件的附加程序并导致应用程序模块之间发生冲突。 Hsving 一个 log4.net 配置简化了管理。
各个模块仍然可以根据每个模块的需要单独进行配置(例如 DB 层的 INFO,UI 层的 ERROR)。每个模块都会通过询问自己的类型来获取记录器:
LogManager.GetLogger(typeof(MyModule);
,但只有 Shell 会使用自己的应用程序配置记录器(例如,调用 XmlConfigurator.Configure) .config。Having separate logger configurations for each module might turn into problems at deployment. Remember that a power user or administrator may completely change the target of your logging, redirecting to a database or to a central repository aggregated logging service (like my company's one). If all separate modules have separate configurations, the power user/admin has to repeat the configuration for each module (in each .config file, or in each module's section in the main app.config), and repeat this every time a change in location/formatting occurs. And besides, given that the appenders are added at run time from configuration and there may be appenders you don't know anything about at the moment, someone may use an appender that locks the file and result in conflict between the app modules. Hsving one single log4.net config simplifies administration.
Individual modules can still be configure as for the needs of each one, separately (eg. INFO for DB layer, ERROR for UI layer). Each module would get the logger by asking for its own type:
LogManager.GetLogger(typeof(MyModule);
but only the Shell will configure the logger (eg. call XmlConfigurator.Configure), using its own app.config.