EDB .NET Connector logging v6.0.2.1

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 EDB Postgres 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 doesn't meet your needs, 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);