1

I have following requirement.

For simplicity Lets say my solution consists of two projects.

  1. A Class library Project wrapping Log4Net Functionality.
  2. A MVC web application project as calling app to class library logger methods which in turn should call log4net actual methods.

I wanted to specify log4net config in the class library app.config file or a xml file in this project.

I read the simlar issue at this thread How do you configure and enable log4net for a stand-alone class library assembly?

But the solution of putting the following two lines in Assembly.info file of my classlibray project didn't work for me.

[assembly: log4net.Config.Repository("ClassLibrary1")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ClassLibrary1.config", Watch = true)]

As Log4Net is unable to find the related configuration.

For records the name of my class library assembly = ClassLibrary1

I am referring this library from my mvc web app project.

Any ideas why the solution2 as in thread How do you configure and enable log4net for a stand-alone class library assembly?

is not working for me?

Edit:

My program flow is like this.

My MVC app starts.

Unity injects a singleton instance of my log4netwrapper through constructor injection successfully.

Then in a action method i make a call to logger through my wrapper. In the wrapper constructor I make the XmlConfigurator.

Please note while calling i am not supplying the fileinfo object as path to read config entry. For that purpose only in my wrapper assembly

[assembly: log4net.Config.Repository("ClassLibrary1")] 
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ClassLibrary1.config", 
                                          Watch = true)]. 

Any solutions here?

Community
  • 1
  • 1
Saurabh
  • 81
  • 4

1 Answers1

1

If you look at the log4net documentation for assembly attributes it says this:

Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

The thing is, when you do this in your MVC project startup, the assembly attributes are in an external assembly, ClassLibrary1.

If you use XmlConfigurator.Configure(...) as in the answer you linked to, it will work.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Sorry but i am not getting your exact steps here. – Saurabh Nov 12 '13 at 02:28
  • My program flow is like this. my mvc app starts. unity injects a singleton instance of my log4netwrapper through constructor injection succesfully. then in a action method i make a call to logger through my wrapper. in the wrapper constructor i make the xmlconfigurator. Please note while calling i am not supplying the fileinfo object as path to read config entry. For that purpose only in my wrapper assembly [assembly: log4net.Config.Repository("ClassLibrary1")] [assembly: log4net.Config.XmlConfigurator(ConfigFile = "ClassLibrary1.config", Watch = true)]. Any solutions here? – Saurabh Nov 12 '13 at 02:34