Monday, January 11, 2010
Global Logging With Shared Business Rules

Many applications require a global log of changes made by users at runtime. Data Aquarium Framework provides an excellent mechanism to create a single point of logging of all operations.

If you create a code file with a class named YourNamespace.Rules.SharedBusinessRules inherited from BusinessRules base then the framework will use this class if there is no custom handler for the data controller.

Here is an example written in C#.

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using MyCompany.Data;

namespace MyCompany.Rules
{
    public class SharedBusinessRules : BusinessRules
    {
        public SharedBusinessRules()
        {
        }

        protected override void AfterSqlAction(ActionArgs args, ActionResult result)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendFormat(@"Controller: {0}, View: {1}, Command: {2}",
                args.Controller, args.View, args.CommandName);
            sb.AppendLine();
            switch (args.CommandName)
            {
                case "Update":
                    foreach (FieldValue v in args.Values)
                        if (v.Modified)
                        {
                            sb.AppendFormat(
                                "Field '{0}' has been changed from '{1}' to '{2}'",
                                v.Name, v.OldValue, v.NewValue);
                            sb.AppendLine();
                        }
                    break;
                case "Insert":
                    foreach (FieldValue v in args.Values)
                        if (v.Modified)
                        {
                            sb.AppendFormat(
                                "Field '{0}' is equal to '{1}'",
                                v.Name, v.Value);
                            sb.AppendLine();
                        }
                    break;
                case "Delete":
                    foreach (FieldValue v in args.Values)
                    {
                        sb.AppendFormat(
                            "Field '{0}' is equal to '{1}'",
                            v.Name, v.Value);
                        sb.AppendLine();
                    }
                    break;

            }
            System.Diagnostics.Debug.WriteLine(sb.ToString());
        }
    }
}

Here is a sample  debug output of the code produced by a Web Site Factory application when records in Northwind.Customers table are updated, inserted, and deleted.

image

The complete debug log in the picture is listed below.

Controller: Customers, View: grid1, Command: Update
Field 'CompanyName' has been changed from 'Alfreds Futterkiste' to 'Alfreds Futterkiste*'

Controller: Customers, View: grid1, Command: Insert
Field 'CustomerID' is equal to 'A'
Field 'CompanyName' is equal to 'New Company'
Field 'ContactTitle' is equal to 'President'

Controller: Customers, View: grid1, Command: Delete
Field 'CustomerID' is equal to 'A    '
Field 'CompanyName' is equal to 'New Company'
Field 'ContactName' is equal to ''
Field 'ContactTitle' is equal to 'President'
Field 'Address' is equal to ''
Field 'City' is equal to ''
Field 'Region' is equal to ''
Field 'PostalCode' is equal to ''
Field 'Country' is equal to ''
Field 'Phone' is equal to ''

You can implement  a database table to keep track of changes instead of sending the output to the Debug log that can be viewed in Visual Studio and Visual Web Developer Express. The code generator will create a user interface for the logging table and this will ensure that you can have an auditing and reporting capability embedded into your web application.

If you need to retain the global logging functionality in the custom data controller handler then simply use SharedBusinessRules class as a base class for your custom data-controller specific business rules.