Question

I am new to Log4Net. i am trying to use this logger for logging my WCF service.

I am having trouble using this , as log file is not getting created.

It is building sucessfully , no error is thrown .

Here is my configuration in app.config:

   <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <section name="Utilities.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
          <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
        </sectionGroup>
    </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <file value="c:\Avinash" />
      <datePattern value="dd.MM.yyyy'.log'" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>

Is there any mistake in it. I was trying this for so much time. But, not able to find out what was the issue.

Any suggestions ??

Also, Here is the class that i have created for logging functionality:

namespace Utilities
{
    public class Logger
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
     (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void log(string strErrorMessage)
        {
            logger.Debug(strErrorMessage);
        }
    }
}   

Is it a correct way to do this ?

I am calling the log method in the exception block where i need to log exception in the service.

please give your ideas about this implementation

Was it helpful?

Solution

<root>
  <appender-ref ref="LogFileAppender" />
</root>

There's you're mistake. It should be:

<root>
  <appender-ref ref="RollingLogFileAppender" />
</root>

OTHER TIPS

You should explicitly initialize your logger before trying to log anything by calling

XmlConfigurator.Configure();

There are another ways to initialize logger (for example, by using attributes); please refer to this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top