Entity Framework 6 Integration
ByteHide Logger provides comprehensive logging for Entity Framework 6 database operations on .NET Framework applications, including SQL commands, connections, and transactions.
Installation
Install the Entity Framework 6 integration package via NuGet:
Install-Package Bytehide.Logger.EntityFramework6
Supported Versions
- .NET Framework 4.6.2
- .NET Framework 4.7.2
- .NET Framework 4.8
Basic Configuration
Initialize ByteHide Logger and Entity Framework 6 integration in your application startup:
using Bytehide.Logger.Core;
using Bytehide.Logger.EntityFramework6;
// Initialize core logger
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
// Initialize EF6 integration
BytehideEntityFrameworkUtility.Initialize(options =>
{
options.ProjectToken = "your-project-token";
options.LogCommands = true;
options.LogConnections = true;
options.LogTransactions = true;
});
In Global.asax (Web Applications)
protected void Application_Start()
{
// Initialize ByteHide Logger
Log.Initialize(new LogSettings
{
MinimumLevel = LogLevel.Info
});
// Initialize EF6 integration
BytehideEntityFrameworkUtility.Initialize(options =>
{
options.ProjectToken = "your-project-token";
});
// Your other initialization code...
}
Configuration Options
Basic Options
BytehideEntityFrameworkUtility.Initialize(options =>
{
// Required
options.ProjectToken = "your-project-token";
// Event Tracking
options.LogCommands = true; // Log SQL queries/commands
options.LogConnections = true; // Log connection events
options.LogTransactions = true; // Log transaction events
// Security
options.IncludeParameterValues = false; // Don't log parameter values
// Performance
options.SlowCommandThresholdMs = 1000; // Flag slow commands (>1s)
options.LogCommandExecutionTime = true; // Include execution times
// Integration
options.CommandLogLevel = LogLevel.Debug; // Log level for commands
options.AutoRegisterInterceptors = true; // Auto-register interceptors
});
Manual Interceptor Registration
If you prefer manual control over interceptor registration:
BytehideEntityFrameworkUtility.Initialize(options =>
{
options.ProjectToken = "your-project-token";
options.AutoRegisterInterceptors = false;
});
// Register interceptors manually when needed
var options = new BytehideEntityFrameworkOptions();
BytehideEntityFrameworkUtility.RegisterInterceptors(options);
Features
Automatic SQL Logging
// This query will be automatically logged
using (var context = new MyDbContext())
{
var users = context.Users.Where(u => u.IsActive).ToList();
}
// Log output:
// [DEBUG] EF6 executing Reader command: SELECT [t0].[Id], [t0].[Name] FROM [Users] AS [t0] WHERE [t0].[IsActive] = @p0
Transaction Tracking
using (var context = new MyDbContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
context.Orders.Add(new Order { /* ... */ });
context.SaveChanges();
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
// Automatically logs:
// - Transaction started
// - Commands executed
// - Transaction committed/rolled back
Slow Query Detection
// Configure slow query threshold
BytehideEntityFrameworkUtility.Initialize(options =>
{
options.ProjectToken = "your-project-token";
options.SlowCommandThresholdMs = 500; // Flag queries >500ms
});
// Slow queries logged as warnings:
// [WARN] EF6 slow Reader command completed in 782ms: SELECT * FROM Products...
Connection Monitoring
// Connection events are automatically logged:
// [DEBUG] EF6 opening database connection to MyDatabase
// [DEBUG] EF6 opened database connection in 25ms
// [DEBUG] EF6 closing database connection to MyDatabase
Usage with Existing Code
ByteHide Logger EF6 integration works with any existing Entity Framework 6 code:
public class OrderService
{
public async Task<Order> GetOrderAsync(int orderId)
{
using (var context = new MyDbContext())
{
// This query will be automatically logged
var order = await context.Orders
.Include(o => o.OrderItems)
.FirstOrDefaultAsync(o => o.Id == orderId);
return order;
}
}
}
Options Reference
Option | Type | Default | Description |
---|---|---|---|
ProjectToken | string | null | Your ByteHide project token |
LogCommands | bool | true | Log SQL queries and commands |
LogConnections | bool | true | Log database connection events |
LogTransactions | bool | true | Log transaction operations |
IncludeParameterValues | bool | false | Include parameter values in logs |
LogCommandExecutionTime | bool | true | Include command execution times |
SlowCommandThresholdMs | int | 1000 | Threshold for slow command warnings (ms) |
CommandLogLevel | LogLevel | Debug | Log level for database commands |
AutoRegisterInterceptors | bool | true | Automatically register EF interceptors |
Best Practices
- Keep
IncludeParameterValues = false
to avoid logging sensitive data - Use
SlowCommandThresholdMs
to identify performance issues - Set appropriate
CommandLogLevel
based on environment (Debug for dev, Info for prod) - Test interceptor registration in your specific EF6 configuration
Example DbContext
public class MyDbContext : DbContext
{
public MyDbContext() : base("DefaultConnection")
{
// ByteHide Logger will automatically intercept operations
}
public DbSet<User> Users { get; set; }
public DbSet<Order> Orders { get; set; }
}
Next Steps
- Configuration - Set up your project token
- Basic Logging - Core logging features
- Exception Handling - Database error logging