我有以下要求。
为简单起见让我们说我的解决方案包含两个项目。
我想在类库app.config文件或此项目中的xml文件中指定log4net config。
我在这个帖子How do you configure and enable log4net for a stand-alone class library assembly?
中读到了simlar问题但是在我的classlibray项目的Assembly.info文件中放入以下两行的解决方案对我来说不起作用。
[assembly: log4net.Config.Repository("ClassLibrary1")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ClassLibrary1.config", Watch = true)]
由于Log4Net无法找到相关配置。
对于记录,我的类库程序集的名称= ClassLibrary1
我从我的mvc web app项目中引用这个库。
为解决方案2在线程How do you configure and enable log4net for a stand-alone class library assembly?
中提出的任何想法对我不起作用?
修改:
我的程序流程是这样的。
我的MVC应用程序启动。
Unity通过构造函数注入成功注入了我的log4netwrapper的单例实例。
然后在一个动作方法中,我通过我的包装器调用logger。在包装器构造函数中,我创建了XmlConfigurator。
请注意,在调用时我不提供fileinfo对象作为读取配置条目的路径。仅用于我的包装程序集
[assembly: log4net.Config.Repository("ClassLibrary1")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ClassLibrary1.config",
Watch = true)].
这里有解决方案吗?
答案 0 :(得分:1)
如果你看log4net documentation for assembly attributes,就会说:
因此,如果使用配置属性,则必须调用log4net 允许它读取属性。一个简单的电话 LogManager.GetLogger将导致调用程序集上的属性 被阅读和处理。 因此必须进行日志记录 在应用程序启动期间尽早调用,以及 当然,在加载和调用任何外部程序集之前。
问题是,当您在MVC项目启动时执行此操作时,外部程序集ClassLibrary1中的程序集属性 。
如果您在链接的答案中使用XmlConfigurator.Configure(...)
,则可以使用。