Thursday 21 October 2010

How to enable logging

Stadium3 uses a third party component 'log4net' for logging.  More details about this component can be found at:
http://logging.apache.org/log4net/

Stadium3 can be configured to log errors or information. Information logging provides stats on pages users have accessed.

It is a very powerful component, and  can be configured to log to the windows event log,  file,  an email address etc.
However because it is so powerful, setting it up and configuration is not very simple.  Below are the steps to follow
to get it working for Stadium3.

By default logging is turned off. In order to turn it on, follow below steps:
  1. Open web.config file, there is a log4Net section as shown below: <log4net>
        <appender name="InformationLog" type="log4net.Appender.FileAppender">
          <file type="log4net.Util.PatternString" value="Logs/InformationLog_%date{yyyyMMdd}.txt"/>
          <appendToFile value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%m%n"/>
          </layout>
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="Off"/>
            <acceptOnMatch value="true"/>
          </filter>
          <filter type="log4net.Filter.DenyAllFilter"/>
        </appender>
        <appender name="InformationLogDatabase" type="log4net.Appender.AdoNetAppender">
          <bufferSize value="1"/>
          <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
          <connectionString value="Data Source=localhost\SqlServer2005;Initial Catalog=Northwind;User=xxx;Pwd=xxxxxxxx"/>
          <commandText value="INSERT INTO EventLog ([Application],[Page],[User],[Details],[LogType]) VALUES (@application,@page,@user,@details,@logtype)"/>
          <parameter>....</parameter>
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="Off"/>
            <acceptOnMatch value="true"/>
          </filter>
          <filter type="log4net.Filter.DenyAllFilter"/>
        </appender>
        <root>
          <appender-ref ref="InformationLog"/>
        </root>
      </log4net>

  2. You can configure to log to  a file or database.  Appender 'InformationLog' logs to a file and 'InformationLogDatabase'
    logs to a database. By default it is configured to log to a file.  This is indicated by element:   <root>
          <appender-ref ref="InformationLog"/>
       </root>

  3. By default logging is turned off, in order to turn it on, you have to change value of the appropriate appender's  <levelToMatch> node.

  4. You can change it to 'Info' for information logging or to 'Error' for error logging. An example of error logging will be: <filter type="log4net.Filter.LevelMatchFilter">
               <levelToMatch value="Error"/>
               <acceptOnMatch value="true"/>
     </filter>

  5. Files are created in a 'Logs' folder in 'StadiumServer' folder, first create this folder, then allow the neccessary user access to this folder, or give  'Everyone' access.

  6. If you want to log to database instead, change to following: <root>
            <appender-ref ref="InformationLogDatabase"/>
     </root>

  7. Errors or information will be logged to an EventLog table, you can configure the database as part of the connectionstring element:  <connectionString value="Data Source=localhost\SqlServer2005;Initial Catalog=Northwind;User=xxx;Pwd=xxxxxxxx"/>

  8. You need to run below script to create this table:
    CREATE TABLE [dbo].[EventLog](
     [ID] [int] IDENTITY(1,1) NOT NULL,
     [Application] [varchar](50) NULL,
     [Page] [varchar](50) NULL,
     [Date] [datetime] NOT NULL DEFAULT (getdate()),
     [User] [varchar](50) NULL,
     [EventType] [varchar](50) NULL,
     [LogType] [varchar](50) NULL,
    [Details] varchar(2000) NULL,
    [Link] varchar(2048) NULL,
     CONSTRAINT [PK_EventLog] PRIMARY KEY CLUSTERED
    (
     [ID] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]

  9. Then change the 'InformationLogDatabase' appender's LevelToMatch tag to 'Error' or 'Info'.

No comments:

Post a Comment