Configuring Log4Net to Register Output from the Class Library

I am trying to configure Log4Net (this is my first time using Log4Net) to enter a text file in an assembly. I am not getting any errors, but it also does not work. I can stop the lines in which I register my result and see that they are achieved, but, as I say, nothing happens.

Where am I going wrong?

I added the following to my packages.config file inside the <packages> attribute:

  <log4net> <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net"> <file value="c:\CTI\log.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="FATAL" /> </filter> </appender> <root> <level value="DEBUG"/> <appender-ref ref="FileAppender"/> </root> </log4net> </configuration> 

I added the following line to AssemblyInfo.cs :

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

I added the Log4Net build using NuGet, and I register as follows:

 private log4net.ILog _Log; _Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); _Log.Debug("FooBar"); 

As I said, there are no errors, but nothing happens.

What am I missing?

+6
source share
1 answer

What is wrong is that you add the log4net configuration section to the nuget configuration file ( packages.config ).

You can have the configuration in the application / network configuration or in a separate file that you point to the appSettings application, for example. the configuration is in a file called config.log4net (the copy to output directory attribute of the file is set to copy always ), and you add the following entry to app / web.config:

 <add key="log4net.config" value="config.log4net"/> 

If you do not want to depend on the configuration of the web application, you can set the ConfigFileExtension XmlConfiguratorAttribute attribute in AssemblyInfo.cs :

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

Then specify the log4net configuration file in the same way as your exe / assembly, as well as the configured extension, for example. MyApplication.exe.log4net or MyLibrary.dll.log4net

Another what's wrong is your appender filter. The range you set excludes the DEBUG level that you plan to register. Here are all logging levels :

 ALL DEBUG INFO WARN ERROR FATAL OFF 

As you can see, DEBUG not between INFO and FATAL .

+6
source

Source: https://habr.com/ru/post/920891/


All Articles