Enabling log4net logging in SLX 72
Posted By: nicocrm on January 20th, 2008 in Programming, Saleslogix
No Gravatar

The Saleslogix web client comes packaged with a wonderful logging system called log4net. This enables us to receive very detailed information about what is going on in every request, how the data is being read from the database, what business rules or events are being executed, etc. Unfortunately it is all disabled in the default installation. While I hope this changes in the near future to a configuration option, here is how you can turn it on for now. Warning: there is a significant performance impact to logging the debugging information – make sure you tweak this before deploying on production (you can restrict it to log only warning or errors).

  • Edit the web.config file, you need to add a line in the top (after the <configSections> tag) that reads:

    <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler,log4net,
        Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
  • At the end of your web.config file (right before the closing </configuration> tag) you will need to paste the log4net configuration proper. I won’t lie, this is a pretty decent chunk of XML and I usually just carry the same one around. But in truth it is pretty simple: we are defining Appenders which is where you want to send the log output (eg to files, or to the event viewers), Loggers which are settings for where the log comes from (for example you can specify that you only want log messages of a certain severity for a given logger, or decide to send messages from some loggers to emails and not others), and finally a root setting which is simply the default settings for all loggers. These are the settings that I use for development – they configure the logger to show only warnings from NHibernate (you can change that to DEBUG but be warned that there is a lot of info and it does have a significant effect on performance at that level), and all messages from any other part of the system. Everything is sent to a text file under the Log subdirectory:
    <!-- This section contains the log4net configuration settings -->
    <log4net debug="false">
      <appender name="rollingFile" type="log4net.Appender.RollingFileAppender" >
    
        <param name="File" value="Log/log.txt" />
        <param name="AppendToFile" value="false" />
        <param name="RollingStyle" value="Date" />
        <param name="DatePattern" value="yyyy.MM.dd" />
        <param name="StaticLogFileName" value="true" />
    
        <layout type="log4net.Layout.PatternLayout">
          <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        </layout>
      </appender>
    
      <!-- Setup the root category, add the appenders and set the
         default priority -->
    
      <root>
        <priority value="DEBUG" />
        <appender-ref ref="rollingFile"/>
      </root>
    
      <logger name="NHibernate">
        <level value="WARN" />
      </logger>
    
      <logger name="NHibernate.SQL">
        <level value="ALL" />
      </logger>
    </log4net>
  • Create a Log directory under the folder and set the permission to be modifiable by Everyone – this is where log4net will send the output.
  • You are almost done, the last thing is you need to create a file called Global.asax in the root of the web client with the following code to initialize the logging system:

    <%@ Application Language="C#" %>
    
    <script runat="server">
    
        private static log4net.ILog _log;
    
        /// <summary>
        /// Initialize web leads library.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>   
        void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            _log = log4net.LogManager.GetLogger(typeof(global_asax));
            _log.Info("Application Started");
        }
    
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            _log.Info("Application Ended");
            log4net.LogManager.Shutdown();
        }
    
        /// <summary>
        /// Redirect to generic error page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        void Application_Error(object sender, EventArgs e)
        {
        }
    
        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
    
        }
    
        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
    
        }
    
    </script>

That’s it! Browse to the client and make sure the “log.txt” file gets created.

There is one more thing I like to do, add this code in the Page_Load of SmartParts/General/GeneralException.ascx, it will give you a traceback in the log whenever you hit an exception (the ones that are reported with the “Saleslogix has encountered an error” screen, not the ones that give you a Yellow Page Of Death):

if (Page.Request.QueryString["exceptionid"] != null)
{
  Exception ex = Sage.Platform.Application.ApplicationContext.Current.State[
     Page.Request.QueryString["exceptionid"]] as Exception;
  if (ex != null)
  {
    ExceptionID.Text = ex.Message;
    log4net.LogManager.GetLogger(typeof(GeneralException)).Warn("Exception Reported", ex);
  }
}

Now in your code, if you want to output some log messages, put code similar to this at the top of your class to declare a logger:

private static readonly log4net.ILog LOG =
  log4net.LogManager.GetLogger(typeof(myclass));

and in your code add calls to LOG.Info, LOG.Debug, etc.

See the log4net home page for more information about this excellent tool.


5 Responses to “Enabling log4net logging in SLX 72”

  1. Ryan Farley says:

    Really grat stuff Nick (as always)

    -Ryan

  2. Nicolas Galler says:

    Thanks! I forgot to mention this – if you see the error pop up in a modal dialog it means it came from an AJAX post instead. In that case you need to edit the code-behind code for the relevant page (e.g. Opportunity.aspx.cs), edit the _scriptManager_AsyncPostBackError function and add a logging statement in there. It does the trick for now but you have to do it on every page! I am hoping to find a more generic way of capturing that in the future.

  3. Nicolas GallerNo Gravatar says:

    Also works on 7.5, but be sure to overwrite the existing Global.asax file – do not just edit it.

  4. Marcos OrfilaNo Gravatar says:

    Hi,

    I configured Log4net as stated here in SLX 7.5.4 and the Log file only gets the following text:

    2012-09-27 18:04:12,368 [VsCBMThreadPool] INFO Sage.Platform.Extensibility.ExtensionManager – Initializing ExtensionManager
    2012-09-27 18:04:12,368 [VsCBMThreadPool] DEBUG Sage.Platform.Extensibility.ExtensionManager – No OrmConfiguration defined

    Looks like something is missing (the OrmConfiguration?). Do you have any advice on this?

    Regards,

    Marcos

    • Alex.CottnerNo Gravatar says:

      Unfortunately this post has pretty stale information about SalesLogix Web 7.2. SLX 7.5.4 web already has a log4net configuration file (log4net.config) that is setup to log against event viewer.

Leave a Reply