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?
- Possibility to join the current log scope if one exists
- Possibility to start a new log scope whenever wanted
- Independence of application layers
- Independence of the environment
- Independence of subscribers
- Have a header
- 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#]
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#]
public LogScope();
public LogScope(LogScope logScope);
public LogScope(object logHeader);
public LogScope(LogScope logScope, object logHeader);
public LogScope(object logMaster, bool requiresNew);
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)