Search code examples
c#log4netglimpse

Not sure how to get Glimpse.Log4Net working


I'm trying to get Glimpse.Log4Net working in an existing ASP.Net MVC v4 project (in VS2010), following the docs here and here, but although the solution compiles and runs Glimpse and log4net seem to be working correctly, I can't see the log4net stuff in the Glimpse window that I'm expecting and as shown near the end of the page in the 2nd link.

I've got dependancy injection going with Autofac using this method, and I don't think it is interfering because I get entries as expected in the log file.

Can anyone point me in the right direction?

In my controller:

public class QuoteController : Controller
{
  private readonly PrintCostEntities db;

  private readonly ILog logger;

  public QuoteController(PrintCostEntities db, ILog logger)
  {
    if (db == null)
    {
      throw new ArgumentNullException("db");
    }

    if (logger == null)
    {
      throw new ArgumentNullException("logger");
    }

    this.db = db;
    this.logger = logger;
  }

  public ActionResult Index()
  {
    this.logger.DebugFormat("Index");

    return this.View(this.db.Quotes);
  }
}

In global.asax:

public class MvcApplication : System.Web.HttpApplication
{
  private readonly ILog logger;

  protected MvcApplication()
  {
    XmlConfigurator.Configure();
    this.logger = LogManager.GetLogger(typeof(MvcApplication));
  }

  protected void Application_Start()
  {
    this.logger.Debug("Application_Start");
  }
}

In web.config:

<configuration>
  <configSections>            
    <section name="glimpse" type="Glimpse.Core.Configuration.Section, Glimpse.Core" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <system.web>
    <httpModules>
      <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" />
    </httpModules>
    <httpHandlers>
      <add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" />
    </modules>
    <handlers>
      <add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
    </handlers>
  </system.webServer>
  <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd" /> 
  <log4net>  
    <appender name="GlimpseAppender" type="Glimpse.Log4Net.Appender.GlimpseAppender">
      <threshold value="ALL" />
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <threshold value="ALL" />
      <file value="log4net.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="5" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>
    <root>
      <appender-ref ref="GlimpseAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
</configuration>

Solution

  • The log4net Glimpse plugin was created by Jess Chadwick and is not yet up to date with the newest version of Glimpse.

    You'll want to be using 1.1 version of Glimpse with MVC4, but the log4net plugin has not been updated yet.

    The good news is that Glimpse.Log4Net is open source, so you should be able to easily update it to work with the new interfaces. I might even give it a go at updating it myself next week.