Log4Net not registered from class library

I am working on the .NET Framework 4.0 using C # on Windows 7 and trying to enter the class library, but it does not work. I run my application without errors, but also nothing happens with my log file, nor on my console.

So here is my code:

This is my App.config :

 <?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <add key="log4net.config" value="config.log4net"/> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG"/> <levelMax value="FATAL"/> </filter> </appender> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="MyLogFile.txt"/> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="5"/> <maximumFileSize value="10MB"/> <staticLogFileName value="true"/> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="debug"/> </filter> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="error"/> </filter> <filter type="log4net.Filter.DenyAllFilter"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="ConsoleAppender"/> <appender-ref ref="RollingFileAppender"/> </root> <logger name="MyApplication"> <level value="DEBUG"/> <appender-ref ref="ConsoleAppender"/> <appender-ref ref="RollingFileAppender"/> </logger> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration> 

Here is what I put into my AssemblyInfo.cs :

 [assembly: log4net.Config.XmlConfigurator(Watch = true)] 

This is what is in the file I'm trying to write:

 private static readonly ILog log = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log.Debug("Testing"); 

When I run my program, nothing happens. Does anyone know why?

+7
class-library log4net
source share
3 answers

Like this answer , you need to place both the app.config, log4net configuration and the AssemblyInfo.cs configurator in the host application.

Suppose I have a Console project as a console project that I run, and Library as a library project. Console will have to have the app.config file with the specified log4net configuration, and AssemblyInfo.cs will need the XmlConfigurator code inside it. Library does not need any of them, and will work by calling LogManager .

 Console > This is the project that you will run. Properties AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here. app.config > Place log4net XML configuration here. Program.cs > Start of application. Library Properties AssemblyInfo.cs > Leave this file alone. Foo.cs > Create the private readonly property. SomeMethod() > log.Debug("Test"); 
+19
source share

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

"Therefore, if you use configuration attributes, you must call log4net so that it can read the attributes. A simple call to LogManager.GetLogger will result in the attributes of the calling assembly for reading and processing. Therefore, you must complete the registration call as soon as the application starts and of course, before how any external assemblies were loaded and called. "

When assembly attributes are defined in the class library, that is, an external assembly, it becomes complex. Can you use log4net.Config.XmlConfigurator.Configure(path) instead?

+3
source share

If someone is still looking at this and to add to the previous answers, you need to create a registrar instance in your host application before you can enter the class library.

+2
source share

All Articles