Question

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>
Was it helpful?

Solution 2

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.

OTHER TIPS

The package has updated to version 1.0 in May 2013 and this version works fine for me in my asp.net mvc4 application.

The log4net tab stays black/inactive (like you mention in your question) when there are no message appended to a GlimpseAppender. There several possible reasons for this:

  • there is no GlimpseAppender configured - this happened to me, because I had a custom log4net configuration
  • There is a redirect being done somewhere in you controller(s), after your message was logged. Your typical RedirectResult will redirect via the client resulting in a new request. The GlimpseAppender only logs message for the current http request, so you will not see any messages from before the redirect. (note: so if you do post-redirect-get, most interesting log message are not in teh glimpse log tab)

And then, of course, there are the "usual" log4net reasons why a message is not logged. But I consider that off-topic for this question.

In case anyone is looking for more info on this issue - what worked for me:

  • VS 2013, MVC4, Glimpse 1.8.5, log4net 1.2.11.0

  • using a separate log4net.config file

  • in AssemblyInfo.cs I had [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] and in Global.asax Application_Start() I had log4net.Config.XmlConfigurator.Configure();

  • no log4net output in Glimpse - the tab was visible but it was blacked out

  • thanks to a tip I found here: https://stackoverflow.com/a/4558906/3590792 I changed the Application_Start() code to:

    string l4net = Server.MapPath("~/log4net.config"); log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));

and Glimpse log4net started working

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top