You are here: Configuration > File Configuration > Storage > Logging Storage

Logging Storage

In this example we will implement a simple file base storage, which will log messages about each IO operation. In the implementation you can see that most of the functionality is derived from StorageDecorator and BinDecorator classes with additional logging added:

LoggingStorage.cs
/**//* Copyright (C) 2004 - 2009  Versant Corporation http://www.versant.com */
using System;
using Db4objects.Db4o.IO;

namespace Db4odoc.Storage
 {
    class LoggingStorage : StorageDecorator
     {
        // delegate to a normal file storage
        public LoggingStorage()
            : base(new FileStorage())
         {

        }

        // use submitted storage as a delegate
        public LoggingStorage(IStorage storage)
            : base(storage)
         {

        }

        /**//**
         * opens a Bin for the given URI.
         */
        public override IBin Open(BinConfiguration config)
         {
            IBin storage = base.Open(config);
            if (config.ReadOnly())
             {
                return new ReadOnlyBin(new LoggingBin(storage));
            }
            return new LoggingBin(storage);
        }

        /**//// <summary>
        /// LoggingBin implementation. Allows to log information
      /// for each IO operation
        /// </summary>
        class LoggingBin : BinDecorator
         {

            public LoggingBin(IBin bin)
                : base(bin)
             {

            }

            // delegate to the base class and log a message
            public override void Close()
             {
                base.Close();
                System.Console.WriteLine(string.Format
("{0} LoggingStorage: File closed", DateTime.Now.ToString()));
            }

            // log a message, then delegate
            public override int Read(long pos, byte[] buffer, int length)
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Reading {1} bytes and {2} position", 
DateTime.Now.ToString(), length, pos));
                return base.Read(pos, buffer, length);

            }

            // log a message, then delegate
            public override void Write(long pos, byte[] buffer, int length)
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Writing {1} bytes and {2} position", 
DateTime.Now.ToString(), length, pos));
                base.Write(pos, buffer, length);
            }

            // log a message, then delegate
            public override void Sync()
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Syncing", DateTime.Now.ToString()));
                base.Sync();
            }

        }
    }
}

LoggingStorage.vb
' Copyright (C) 2004 - 2009  Versant Corporation http://www.versant.com 

Imports System
Imports Db4objects.Db4o.IO

Namespace Db4odoc.Storage
    Class LoggingStorage
        Inherits StorageDecorator
        Public Sub New()
            ' delegate to a normal file storage
            MyBase.New(New FileStorage())

        End Sub

        Public Sub New(ByVal storage As IStorage)
            ' use submitted storage as a delegate
            MyBase.New(storage)

        End Sub

        '
        ' opens a logging bin for the given URI.
        '         
        Public Overloads Overrides Function Open(ByVal config _ 
As BinConfiguration) As IBin
            Dim storage As IBin = MyBase.Open(config)
            If config.[ReadOnly]() Then
                Return New ReadOnlyBin(New LoggingBin(storage))
            End If
            Return New LoggingBin(storage)
        End Function

        ' LoggingBin implementation. Allows to log information
        ' for each IO operation
        Private Class LoggingBin
            Inherits BinDecorator

            Public Sub New(ByVal bin As IBin)
                MyBase.New(bin)

            End Sub

            ' delegate to the base class and log a message
            Public Overloads Overrides Sub Close()
                MyBase.Close()
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: File closed", DateTime.Now.ToString()))
            End Sub

            ' log a message, then delegate
            Public Overloads Overrides Function Read(ByVal _ 
pos As Long, ByVal buffer As Byte(), _  
ByVal length As Integer) As Integer
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: Reading {1} bytes and {2} position", _ 
DateTime.Now.ToString(), length, pos))

                Return MyBase.Read(pos, buffer, length)
            End Function

            ' log a message, then delegate
            Public Overloads Overrides Sub Write(ByVal pos As Long, _ 
ByVal buffer As Byte(), ByVal length As Integer)
                System.Console.WriteLine(_ 
String.Format("{0} LoggingStorage: Writing {1} bytes and {2} position", _ 
DateTime.Now.ToString(), length, pos))
                MyBase.Write(pos, buffer, length)
            End Sub

            ' log a message, then delegate
            Public Overloads Overrides Sub Sync()
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: Syncing", DateTime.Now.ToString()))
                MyBase.Sync()
            End Sub

        End Class
    End Class
End Namespace

Use the LoggingStorage implementation with the following code (you can find a working example if you download LoggingStorage class).

C#:

config.File.Storage(new LoggingStorage());

VB.NET:

config.File.Storage(New LoggingStorage())

Download example code:

VB.NET c#