Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

Developing a Log Scope – Part I: Developing the Concept

3.26/5 (14 votes)
15 Apr 2010CPOL1 min read 1  
Introducing the concept

Introduction

In most enterprise applications, logging is as essential as any other business need. An internet banking site, for example, can't live without logs.

What is Logging?

In my view, an application log is a record of all events and operations on the model with the information needed to provide an audit trail.

The recorded information should be such that could be used to rollback and/or replay all or selected operations.

A good example of this concept is SQL Server’s transaction log.

What is Tracing?

Tracing, on the other hand, is a record of events in the application itself.

The recorded information can be used to track application behaviors and should never be critical to the business.

Still having in mind the SQL Server analogy, tracing is what SQL Server logs in the Windows EventLog.

What is a Log Scope?

A log scope is scope of events and operations that need to be recorded as a unit.

A log scope can be the request/response of a web page to a web server, can be a call to a web service, can be retrieving some information from a database, etc.

What are the Prerequisites for a Log Scope?

  1. Possibility to join the current log scope if one exists
  2. Possibility to start a new log scope whenever wanted
  3. Independence of application layers
  4. Independence of the environment
  5. Independence of subscribers
  6. Have a header
  7. Have multiple details

Usage Pattern

The best usage pattern would be a proxy class that implements IDisposable which would allow the use of using (C#, VB) blocks, improving the code readability and maintainability.

C#
[C#]
using (LogScope log = new LogScope())
{
  ...
  log.Publish(message);
  ...
}
[VB.NET]
Using log as New LogScope()
  ...
  log.Publish(message)
  ...
End Using

Creating Log Scope Proxies

The creation of log scope proxy instances should be by means of instantiation of a class instance with the proxy set up parameters.

C#
[C#]
/// <summary>
/// Initializes a new log scope proxy that joins the current log scope, if one exists.
/// </summary>
public LogScope();

/// <summary>
/// Initializes a new log scope proxy that joins a specified log scope, if one exists.
/// </summary>
/// <param name="logScope">The log scope to join.<//param>
public LogScope(LogScope logScope);

/// <summary>
/// Initializes a new log scope proxy that starts a new log scope 
/// from the current log scope.
/// </summary>
/// <param name="logHeader">The log header.<//param>
public LogScope(object logHeader);

/// <summary>
/// Initializes a new log scope proxy that starts a new log scope 
/// from the specified log scope.
/// </summary>
/// <param name="logScope">The log scope.<//param>
/// <param name="logHeader">The log header.<//param>
public LogScope(LogScope logScope, object logHeader);

/// <summary>
/// Initializes a new log scope proxy from the current log scope.
/// </summary>
/// <param name="logMaster">The log master.<//param>
/// <param name="requiresNew">If set to <see langword="true" />, 
/// starts a new log scope.<//param>
public LogScope(object logMaster, bool requiresNew);

/// <summary>
/// Initializes a new log scope proxy from the specified log scope.
/// </summary>
/// <param name="logScope">The log scope.<//param>
/// <param name="logHeader">The log header.<//param>
/// <param name="requiresNew">If set to <see langword="true" />, 
/// starts a new log scope.<//param>
public LogScope(LogScope logScope, object logHeader, bool requiresNew);

[VB.NET]
''' <summary>
''' Initializes a new log scope proxy that joins the current log scope, if one exists.
''' </summary>
Public Sub New()

''' <summary>
''' Initializes a new log scope proxy that joins a specified log scope, if one exists.
''' </summary>
''' <param name="logScope">The log scope to join.<//param>
Public Sub New(ByVal logScope As LogScope)

''' <summary>
''' Initializes a new log scope proxy that starts a new log scope 
''' from the current log scope.
''' </summary>
''' <param name="logHeader">The log header.<//param>
Public Sub New(ByVal logHeader As Object)

''' <summary>
''' Initializes a new log scope proxy that starts a new log scope 
''' from the specified log scope.
''' </summary>
''' <param name="logScope">The log scope.<//param>
''' <param name="logHeader">The log header.<//param>
Public Sub New(ByVal logScope As LogScope, ByVal logHeader As Object)

''' <summary>
''' Initializes a new log scope proxy from the current log scope.
''' </summary>
''' <param name="logMaster">The log master.<//param>
''' <param name="requiresNew">If set to <SEE langword="true" />, 
''' starts a new log scope.<//param>
Public Sub New(ByVal logMaster As Object, ByVal requiresNew As Boolean)

''' <summary>
''' Initializes a new log scope proxy from the specified log scope.
''' </summary>
''' <param name="logScope">The log scope.<//param>
''' <param name="logHeader">The log header.<//param>
''' <param name="requiresNew">If set to <SEE langword="true" />, 
''' starts a new log scope.<//param>
Public Sub New(ByVal logScope As LogScope, _
	ByVal logHeader As Object, ByVal requiresNew As Boolean)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)