1

I am using Common.Logging.Log4Net.Universal for writting logs in xml format, so that I can read it using YALV! However, I can not see the logs are getting generated for this. I have used this technique for normal log4net library and works without any issues. Here is the relevant configuration in my 'web.config' file:

 <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.Universal.Log4NetFactoryAdapter, Common.Logging.Log4Net.Universal" >
        <arg key="configType" value="FILE"/>
        <arg key="configFile" value="~/log4net.config" />
      </factoryAdapter>
    </logging>
  </common>

And here goes my 'log4net.config' file:

<configuration>
    <log4net>
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="C://Logs//mylogfile.xml" type="log4net.Util.PatternString"/>
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
          <locationInfo value="true"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="INFO" />
            <levelMax value="FATAL" />
          </filter>
      </appender>
      <root>
        <level value="ALL"/>
        <appender-ref ref="FileAppender"/>
      </root>
  </log4net>
</configuration>

Now below is the code for actually generating the logs:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyNamespace
{
    public class HomeController : Controller
    {
        private static readonly ILog Log = new Log4NetFactoryAdapter().GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public ActionResult Index()
        {
            Log.Error(m=>m("My First Logs"));
            return View();
        }
    }
}

Upon executing, code execute without any error, however the above mentioned "mylogfile.xml" never gets generated.

Please advice, in case somebody have tried the same.

1 Answers1

2

When I look at the GitHub source of the Universal logging project I see the following configuration for the web.config:

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="common">
  <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
</configSections>
<common>
<logging>
  <factoryAdapter type="Common.Logging.Log4Net.Universal.Log4NetFactoryAdapter, Common.Logging.Log4Net.Universal" />
</logging>
 </common>
 </configuration>

And then you should remove the configuration root element from the 'log4net.config' file:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="C://Logs//mylogfile.xml" type="log4net.Util.PatternString"/>
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
      <locationInfo value="true"/>
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
  </appender>
  <root>
    <level value="ALL"/>
    <appender-ref ref="FileAppender"/>
  </root>
</log4net>

If that is not working, enable debugging for log4net to see what is going wrong with your configuration.

Peter
  • 27,590
  • 8
  • 64
  • 84