EDB .NET Connector Logging v4.1.6.1

Edit this page

EDB .NET Connector supports the use of logging to help resolve issues with the .NET Connector when used in your application. The connector uses classes in the EnterpriseDB.EDBClient.Logging namespace for logging.

Console Logging to Standard Error

EnterpriseDB.EDBClient.Logging.ConsoleLoggingProvider is a class that outputs error messages to STDERR. To use this class, include the following line in your application before using any of the Advanced Server .NET Connector APIs.

EnterpriseDB.EDBClient.Logging.EDBLogManager.Provider = new EnterpriseDB.EDBClient.Logging.ConsoleLoggingProvider(EDBLogLevel.Debug, true, true);

The following log levels are available:

  • Trace
  • Debug
  • Info
  • Warn
  • Error
  • Fatal

Writing a Custom Logger

If the console logging provider does not fulfill your requirements, you can write a custom logger by implementing the EnterpriseDB.EDBClient.Logging.IEDBLoggingProvider interface, and extending the EnterpriseDB.EDBClient.Logging.EDBLogger class, for instance, writing your logs to a file. The following is a simple example of how to write a custom logger:

public class MyLoggingProvider : IEDBLoggingProvider
    {
        string _logFile;
        readonly EDBLogLevel _minLevel;
        readonly bool _printLevel;
        readonly bool _printConnectorId;

        public MyLoggingProvider(string logFile, EDBLogLevel minLevel = EDBLogLevel.Info, bool printLevel = false, bool printConnectorId = false)
        {
            _logFile = logFile;
            _minLevel = minLevel;
            _printLevel = printLevel;
            _printConnectorId = printConnectorId;
        }

        public EDBLogger CreateLogger(string name)
        {
            return new MyLogger(_logFile, _minLevel, _printLevel, _printConnectorId);
        }
    }

 class MyLogger : EDBLogger
    {
        string _logFile;
        readonly EDBLogLevel _minLevel;
        readonly bool _printLevel;
        readonly bool _printConnectorId;

        internal MyLogger(string logFile, EDBLogLevel minLevel, bool printLevel, bool printConnectorId)
        {
            _logFile = logFile;
            _minLevel = minLevel;
            _printLevel = printLevel;
            _printConnectorId = printConnectorId;
        }

        public override bool IsEnabled(EDBLogLevel level) => level >= _minLevel;

        public override void Log(EDBLogLevel level, int connectorId, string msg, Exception exception = null)
        {
            if (!IsEnabled(level))
                return;


            using (StreamWriter writer = new StreamWriter(_logFile, true))
            {
                var sb = new StringBuilder();
                if (_printLevel)
                {
                    sb.Append(level.ToString().ToUpper());
                    sb.Append(' ');
                }

                if (_printConnectorId && connectorId != 0)
                {
                    sb.Append("[");
                    sb.Append(connectorId);
                    sb.Append("] ");
                }

                sb.AppendLine(msg);

                if (exception != null)
                    sb.AppendLine(exception.ToString());

                writer.Write(sb.ToString());
            }

        }
    }

To use this custom logger, put the following line in your application before using any of the EDB .NET Connector APIs:

EDBLogManager.Provider = new MyLoggingProvider(filepath, EDBLogLevel.Debug, true, true);