EDB .NET Connector logging v7.0.4.1

EDB .NET Connector supports the use of logging to help resolve issues with the .NET Connector when used in your application. EDB .NET Connector supports logging using the standard .NET Microsoft.Extensions.Logging package. For more information about logging in .Net, see Logging in C# and .NET.

Note

For versions earlier than 7.x, EDB .NET Connector had its own, custom logging API.

Console logging provider

.NET logging API works with a variety of built-in and third-party logging providers. The console logging provider logs output to the console.

Console logging with EDBDataSource

Create a Microsoft.Extensions.Logging.LoggerFactory and configure an EDBDataSource with it. Any use of connections opened through this data source log using this logger factory.

var loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole());

var dataSourceBuilder = new EDBDataSourceBuilder(connectionString);
dataSourceBuilder.UseLoggerFactory(loggerFactory);
await using var dataSource = dataSourceBuilder.Build();

await using var connection = await dataSource.OpenConnectionAsync();
await using var command = new EDBCommand("SELECT 1", connection);
_ = await command.ExecuteScalarAsync();

Console logging without EDBDataSource

Create a Microsoft.Extensions.Logging.LoggerFactory and configure EDB .NET Connector's logger factory globally using EDBLoggingConfiguration.InitializeLogging. Configure it at the start of your program, before using any other EDB .NET Connector API.

var loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole());
EDBLoggingConfiguration.InitializeLogging(loggerFactory);

await using var conn = new EDBConnection(connectionString);
await conn.OpenAsync();
await using var command = new EDBCommand("SELECT 1", conn);
_ = await command.ExecuteScalarAsync();

Log levels

The following log levels are available:

  • Trace
  • Debug
  • Information
  • Warning
  • Error
  • Fatal

This example shows how to change the log level to Trace:

var loggerFactory = LoggerFactory.Create(builder => builder
.SetMinimumLevel(LogLevel.Trace
.AddSimpleConsole()
);

Formatting the log output

This example shows how to format your log output. Create a LoggerFactory to restrict each log message to a single line and add a date time to the log:

var loggerFactory = LoggerFactory.Create(builder =>
builder
.SetMinimumLevel(LogLevel.Trace)
.AddSimpleConsole(
	options =>
	{
		options.SingleLine = true;
		options.TimestampFormat = "yyyy/MM/dd HH:mm:ss ";
	}
	));