Log4Net section in Web.Config generates an error

Therefore, I am trying to configure Log4Net in my Web.NET 4.0 application. I added the correct .dll file to my project and added the following to my Web.Config file:

<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> </configSections> <log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:\\TestProj\\TestLog.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" /> </layout> </appender> 

However, if I add the "log4net" section to Web.Config, I get an error message

Unable to start debugging on the web server. See Help for general configuration issues .....

Verify that the server is working correctly. Make sure there is no error syntax in web.config ........

Note I can delete all the internal parts of this section and leave only the announcement:

 <log4net></log4net> 

and I still get the same error.

Can someone give me some pointers on how to track this error?

+7
source share
2 answers

Take a look at the sample configurations on the log4net page. Most likely you have lost the required tags.

+1
source

For developers who don’t know exactly how to get started, it may help

Configuration in app.config

Remember to tell your application that the library represents the custom configuration section that you are going to use, I'm not quite sure if this is required or not, but I always use it as the first section in the root <configuration> .

 <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> 

log4net config in app.config

There are many different applications in log4net, but I usually use RollingFileAppender, so I use the same one in this example, you can find the rest here .

 <log4net> <!-- file appender --> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:/logs/my_log_file.log"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <maxSizeRollBackups value="30"/> <datePattern value=".yyyy-MM-dd"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="RollingFileAppender"/> </root> </log4net> 

Update AssemblyInfo.cs file

I always skip this step when I need to create a new project. Therefore, remember that you must tell your application to monitor the XMLConfigurator to complete the log4net configuration, so the following line goes at the end of the AssemblyInfo.cs file:

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

Beginning of work

Remember to include the log4net.dll link, then use the following line of code to initialize the log in the class

 private static ILog log = LogManager.GetLogger(typeof(MyClass)); 

And in the end allows you to use it, as after

 log.Info("Hello log4net"); 

Happy magazine :)

+13
source

All Articles