0

I am using visual studio 2010, for logging I wanted to create common library project of my own to use both log4net and elmah and call it from any of my other project, could be another library project or web service or web application. So, using NuGet I have installed common.logging and common.logging.log4net and elmah in my App.Common c# library project and added the following in app.config of App.Common project.

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>

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

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <log4net>
    <root>
      <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value=".\\App_Data\\log.txt"/>
        <appendToFile value="true" />
        <datePattern value=".yyyy-MM-dd'.log'"/>
        <maxSizeRollBackups value="365"/>
        <staticLogFileName value="false"/>
        <rollingStyle value="Date" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

        <filter type="log4net.Filter.LevelRangeFilter">
          <acceptOnMatch value="true" />
          <levelMin value="INFO" />
          <levelMax value="FATAL" />
        </filter>

        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%newline[%date{dd MMM yyyy HH:mm:ss.fff}](%thread) %-5level: %message (%location)" />
        </layout>
      </appender>

      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>  
</configuration>

And my logger class is like the following:

private static log4net.ILog logManager = null;

public static log4net.ILog GetLogManager() {
  if (logManager == null) {
    logManager = LogManager.GetLogger("DentalAppLogger");
  }

  return logManager;
}

/// <summary>
/// Log in both Elmah and log4net. 
/// </summary>
/// <param name="exc"></param>
/// <param name="message"></param>
public static void Log(string message, Exception exc = null) {
  if (exc == null) {
    exc = new ApplicationException(message);
  }

  // Log to log4net
  GetLogManager().Error(message, exc);
  // Log to ELMAH
  if (HttpContext.Current != null) ErrorSignal.FromCurrentContext().Raise(exc, ystem.Web.HttpContext.Current);
}

I have created a divide by zero exception and called it from my App.Webservice to check the log function but log file is not created in caller AppData folder. Am I missing something?

t0mppa
  • 3,983
  • 5
  • 37
  • 48
aman19161
  • 51
  • 7
  • 2
    Do you call [`XmlConfigurator.Configure()`](http://stackoverflow.com/questions/3618380/log4net-does-not-write-the-log-file?rq=1), have you [enabled internal debugging](http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems) so you can see what log4net is trying to do, does the [user running the application have permissions to write the file](http://stackoverflow.com/questions/2796047/log4net-configuration-problem), did you [use the search](http://stackoverflow.com/search?q=log4net+not+writing)? – CodeCaster Feb 17 '14 at 13:28
  • make sure that it have the write/modify rights on the directory – OuSs Feb 17 '14 at 13:32
  • Thank you @CodeCaster, I added log4net.config in my webservice and added [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] in my App.Common AssemblyInfo.cs, and its working now :). Thank you again. – aman19161 Feb 18 '14 at 02:18

0 Answers0