Sunday, January 10, 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.

6 comments:

Albert Alves said...

Audit Trail...

This is a very important feature. How can I send the log information to a database table?

Can you show in the C# example provided?

Customer Service said...

Albert,

You can simply replace the following line with code to put the result of sb.ToString() into a database table.

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

John Li said...

Hi, I'm testing this global logging. There's a issue that only one handler can be specified right? How to use both shared business rules and the custom rules? I mean combine them together.

Customer Service said...

New option "Generate a shared business rules class to implement global logging of actions executed by data controllers of your application. " is available on business logic layer page of project wizard.

If this option is enabled then a a global shared business rules class is created.

If a custom business rules is created the it will automatically inherit SharedBusinessRules if the option is enabled in your project.

Gonzalo Aguilera said...

Do you have a VB version for your sample?

Stinkbutt said...

here is the VB.NET version of the above example

Protected Overrides Sub AfterSqlAction(ByVal args As ActionArgs, ByVal result As ActionResult)
Dim sb As New StringBuilder()
sb.AppendLine()
sb.AppendFormat("Controller: {0}, View: {1}, Command: {2}",
args.Controller, args.View, args.CommandName)
sb.AppendLine()
Select (args.CommandName)

Case "Update"
For Each v As FieldValue In args.Values
If (v.Modified) Then
sb.AppendFormat(
"Field '{0}' has been changed from '{1}' to '{2}'",
v.Name, v.OldValue, v.NewValue)
sb.AppendLine()
End If
Next
Case "Insert"
For Each v As FieldValue In args.Values
If v.Modified Then
sb.AppendFormat(
"Field '{0}' is equal to '{1}'",
v.Name, v.Value)
sb.AppendLine()
End If
Next
Case "Delete"
For Each v As FieldValue In args.Values
sb.AppendFormat(
"Field '{0}' is equal to '{1}'",
v.Name, v.Value)
sb.AppendLine()
Next
End Select
System.Diagnostics.Debug.WriteLine(sb.ToString())
End Sub

You can find more about Code OnTime Generator, Data Aquarium Framework, and other great products here.


© 2010 Code OnTime LLC. Intelligent code generation software for ASP.NET. Visit us at http://codeontime.com