Jimmyt1988
Jimmyt1988

Reputation: 21126

Appenders list empty in Logger for Log4net

I have the following config:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{ABSOLUTE} 
    [%thread] %level %logger - %message%newline"/>
    </layout>
  </appender>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\Log.txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{ABSOLUTE} 
    [%thread] %level %logger - %message%newline" />
    </layout>
    <encoding value="utf-8" />
  </appender>

  <logger name="TraceLogger" additivity="false">
    <level value="INFO" />
    <appender-ref ref="RollingFileAppender"/>
    <appender-ref ref="ConsoleAppender" />
  </logger>
</log4net>

I have run Configure on the XmlConfigurator:

static class ProgramEntry
{
    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        ...

My web.config has:

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

...

<log4net configSource="Config\log4net.config" />

...




<system.diagnostics>
    <trace autoflush="true" indentsize="0">
      <listeners>
        <add name="Log4NetTraceListener" type="BLAH.Log4netTraceListener, BLAH" />
      </listeners>
    </trace>
  </system.diagnostics>

I have a Trace listener implementation:

public class Log4netTraceListener : System.Diagnostics.TraceListener
{
    private readonly ILog log;

    public Log4netTraceListener()
    {
        log = LogManager.GetLogger("TraceLogger");

        // disable logging to trace write if logging fails, or else we'll end up in a deadlock.
        log4net.Util.LogLog.EmitInternalMessages = false;
    }

    private string level = "Info";

    public override void Write(string message)
    {
        if (message.Contains("Error:"))
        {
            level = "Error";
        }
        else if (message.Contains("Warning:"))
        {
            level = "Warning";
        }
        else if (message.Contains("Information:"))
        {
            level = "Info";
        }
        else
        {
            if (log != null)
            {
                LogMessage(message);
            }
        }
    }

    public override void WriteLine(string message)
    {
        if (log != null)
        {
            LogMessage(message);
        }

        // reset the level
        level = "Info";
    }

    private void LogMessage(string message)
    {
        if (level == "Info")
        {
            log.Info(message);
        }

        if (level == "Error")
        {
            log.Error(message);
        }

        if (level == "Warning")
        {
            log.Warn(message);
        }
    }
}

The Trace listener gets run, the "TraceLogger" seems to get found, but its appenders collection is empty.

enter image description here

What might i be doing wrong? The real issue is that

EDIT

Found an error:

log4net:ERROR Failed to parse config file. Is the specified as: System.Configuration.ConfigurationErrorsException: Unable to open configSource file 'Config\log4net.config'. (C:\Users\awdawd\Documents\Visual Studio 2015\Projects\BLAH\BLAH\BLAH\bin\Debug\DataFactoryPreparationManager.exe.Config line 36)

My app.config is in the root directory, my Config folder is underneath this directory

Upvotes: 0

Views: 1710

Answers (1)

Andy Lamb
Andy Lamb

Reputation: 2221

Make sure log4net.config is copied to bin\Debug\Config via the 'Copy to Output Directory' property of the file from within Visual Studio. Right click on the file in the Solution Explorer and select 'Properties'.

Upvotes: 2

Related Questions