NDatabase Logging


  • To turn on a logging, the first step is to invoke EnableLogging() method on NDatabase.Odb.OdbConfiguraiton.
  • Then you need to add your custom logger to NDatabase, or you can just add internal console logger. The custom logger needs to implement NDatabase.Tool.ILogger interface.

Using internal console logger

To enable internal console logger, we just need to invoke one method. This method will add the logger into NDatabase engine and automatically it will turn on logging for NDatabase.

OdbConfiguration.EnableConsoleLogger();

Using custom logger

To use your logging library with NDatabase, you need to add own custom logger implementation and then register it through OdbConfiguration class. Again, that will automatically turn on logging for NDatabase.

Below you have log4net custom logger, which allows us to reuse existing log4net framework for logging purposes in NDatabase

public class Log4NetLogger : ILogger
{
    private static readonly ILog Log = LogManager.GetLogger(typeof (ILogger));

    #region Implementation of ILogger

    public void Warning(string message)
    {
        Log.Warn(message);
    }

    public void Debug(string message)
    {
        Log.Debug(message);
    }

    public void Info(string message)
    {
        Log.Info(message);
    }

    public void Error(string message)
    {
        Log.Error(message);
    }

    public void Error(string message, Exception t)
    {
        Log.Error(message);
        Log.ErrorFormat("Error: {0}, exception: {1}", t.Message, t);
    }

    #endregion
}

Then we need to register this class instance:
OdbConfiguration.RegisterLogger(new Log4NetLogger());

Enabling/Disabling NDatabase logging

To enable or disable logging in any time you can use two methods of OdbConfiguration class:
OdbConfiguration.EnableLogging();
OdbConfiguration.DisableLogging();

Last edited Dec 28, 2012 at 1:30 PM by jacek, version 11

Comments

No comments yet.