user476566
user476566

Reputation: 1349

How can I see logs from Log4net?

I don't see any logging being done for my website. The logging is configured as follows in web.config

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

 <log4net>
 <logger name="default">
   <level value="INFO"></level>
 </logger>

 <appender name="RollingLogFileAppender" 
            type="log4net.Appender.RollingFileAppender">
   <file value="logs\Tester.log"/>
   <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
   <appendToFile value="true"/>
   <rollingStyle value="Composite"/>
   <datePattern value=".yyyyMMdd"/>
   <maximumFileSize value="10MB"/>
   <countDirection value="1"/>
   <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date [%thread] %-5level %logger
            [%property{NDC}] - %message%newline"/>
   </layout>      
 </appender>

 <root>
   <level value="INFO"></level>
 </root>

 <logger name="Test">
   <level value="DEBUG"/>
   <appender-ref ref="RollingLogFileAppender"/>
 </logger>

Here is instantiation.

public class AppLog
    {
        private static readonly log4net.ILog log = 
               log4net.LogManager.GetLogger("Test");

        static public void logEvent(String msg, EventLogEntryType type)
        {
            switch (type)
            {
                case EventLogEntryType.Error:
                    log.Error(msg);
                    break;
                case EventLogEntryType.Information:
                    log.Info(msg);
                    break;
                case EventLogEntryType.Warning:
                    log.Warn(msg);
                    break;
                default:
                    log.Debug(msg);
                    break;
            }
        }
    }

On Application start, I write some log information

 void Application_Start(object sender, EventArgs e)
     {
         // Code that runs on application startup
         AppLog.logEvent("Application Error:  " , EventLogEntryType.Error);

         AppLog.logEvent("Application Error:  ", EventLogEntryType.FailureAudit);
         AppLog.logEvent("Application Error:  ", EventLogEntryType.Information);
         AppLog.logEvent("Application Error:  ", EventLogEntryType.SuccessAudit);
         AppLog.logEvent("Application Error:  ", EventLogEntryType.Warning);

     }

I don't see any log. I was hoping the Tester.log will be created in C:\logs; I even searched entire C: drive, but looks like logging is not working for me.

Upvotes: 0

Views: 1471

Answers (2)

Rob Levine
Rob Levine

Reputation: 41358

You need configure the logger with the configurator. This is what actually loads your xml configuration and applies it to log4net.

You can either configure log4net with an assembly attribute (e.g. in the AssemblyInfo.cs file):

[assembly: log4net.Config.XmlConfigurator]

or you can manually call the configurator from your code:

XmlConfigurator.Configure();

Upvotes: 1

krivtom
krivtom

Reputation: 24916

You need to call

XmlConfigurator.Configure();

before you start using loggers

Upvotes: 1

Related Questions