Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have used the following URL to implement log4net,
[code_project], Can anyone please help me to understand step 6?
C#
if (log.IsErrorEnabled)
{
    log.Error("Page Load failed : " + ex.Message);
}

I don't understand how if (log.IsErrorEnabled) works...

Regards
Posted

The property for IsErrorEnabled checks your app.config file to see if you have that level of logging turned on. You can turn on the logging like this to have error logging on. You would also have to have an appender installed named EventLogAppender. But the level can be changed to just WARN or INFO or DEBUG so that it will only log above that level. So in this example it logs for errors only. If it was using WARN then a call to check for errors would still return true since it is a higher level.

XML
<root>
  <level value="ERROR"/>
  <priority value="ERROR"/>
  <appender-ref ref="EventLogAppender"/>
</root>


If you need more help with log4net, let me know.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Mar-12 12:44pm    
+5. Well Explained.
Sebastian T Xavier 22-Mar-12 0:34am    
Steve, I hope you have seen my query below.. please help ...
Yes Steve, I still have doubt..

I have added the following code in my web.config file
HTML
<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="2KB" />
        <staticlogfilename value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionpattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
     </appender>
     <root>
	 <level value="INFO" />
	<appender-ref ref="RollingLogFileAppender" />
     </root>
</log4net>


And added the following code in the .cs file
C#
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("This a test debug message");


In this case nothing is getting written in the file. What I understood from the above reply is, it should log the content to the file as I am not checking any conditions, even if the log level is INFO.

I think the above code should write something to log file and the following code shouldn't, as there is a check.
C#
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    if (log.IsDebugEnabled)
    {
       log.Debug("This a test debug message");
    }


Can you help me on this?

Thanks
 
Share this answer
 
Comments
Steve Maier 22-Mar-12 11:21am    
The log4net routines also check if debug or info are enabled as well. So Even tho you did not put the check in, log4net did it for you in the log.Debug method. The reason that alot of people use the if statement is that then they might not do any string manipulation as well.

please also keep in mine that using reflecktion to get the logger is not the fastest way. Reflection can be slow and if you are putting it in every method, I can understand wanting all of that to be the same, but using reflection might slow you down.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900