Search code examples
c#wcflog4net

Log4Net logger class not creating file when called from WCF Service Library Project?


Have this Log4net class in a class library project. When accessed from a windows application after using the .dll, the file is created and log is written.

When using the same .dll of my class library project in my WCF service library project, the file is not created at all.

The way i write to the log from windows application and the wcf service library project is exactly same as below.

Why is the behavior different when called from WCF ?

How I call the logger class from other projects in the same solution :

public Logger Logger;
Logger = Logger.Instance();
Logger.Debug("Product Service is called");

Logger Class in Class Library Project :

    public class Logger
{
    private static volatile Logger instance;
    private static object syncRoot = new Object();
    private static log4net.ILog logger = null;
    private object _lock = new object();

    private Logger()
    {
        if (logger == null)
        {
            XmlConfigurator.Configure();
            logger = log4net.LogManager.GetLogger("LoggerManager");
        } 
    }

    public static Logger Instance()
    { 
        if (instance == null)
        {
            lock (syncRoot)
            {
                if (instance == null)
                    instance = new Logger();
            }
        } 
        return instance;
    } 


    public void Info(string info, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
    {
        lock (_lock)
        {
            logger.Info(string.Format("Info : {0} in member {1}, file {2} at line {3}", info, memberName, filePath, lineNumber));
        }
    }
}

log4net.config.xml :

  <log4net debug="false">
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="c:\Logs\App.Log" />
    <threshold value="ALL" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maximumFileSize value="1MB" />
    <maxSizeRollBackups value="10" />
    <datePattern value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="*%-10level %-30date %message [%logger] [%thread] %newline" />
    </layout>
  </appender>
  <logger name="Logger">
    <level value="ALL"/>
    <appender-ref ref="RollingFileAppender"/>
  </logger> 

  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFile" /> 
  </root>
</log4net>

Solution

  • Do you have the <log4net> section in the appropriate app.config file for your WCF service? If it is missing, the log file will not be created.

    Edit

    After tracking down the problem in a chat room, the solution was to copy the missing log4net.config.xml in the output directory of the WCF dll. Then the logfile appeared.