-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
232 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...yFramework/Auditing/IAuditInfoProvider.cs → ...s.EntityFramework/IContextInfoProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Shiny.Extensions.EntityFramework/QueryLog/QueryLogDbCommandInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Data.Common; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace Shiny.QueryLog; | ||
|
||
|
||
public class QueryLogDbCommandInterceptor(IContextInfoProvider infoProvider, TimeSpan minLogDuration) : IDbCommandInterceptor | ||
{ | ||
public int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result) | ||
{ | ||
this.TryLog(command, eventData); | ||
return result; | ||
} | ||
|
||
|
||
public DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result) | ||
{ | ||
this.TryLog(command, eventData); | ||
return result; | ||
} | ||
|
||
|
||
public object? ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object? result) | ||
{ | ||
this.TryLog(command, eventData); | ||
return result; | ||
} | ||
|
||
|
||
protected void TryLog(DbCommand command, CommandExecutedEventData eventData) | ||
{ | ||
if (minLogDuration < eventData.Duration) | ||
{ | ||
var log = new QueryLogEntry | ||
{ | ||
UserIdentifier = infoProvider.UserIdentifier, | ||
UserIpAddress = infoProvider.UserIpAddress, | ||
AppLocation = infoProvider.AppLocation, | ||
|
||
Query = eventData.Command.CommandText, | ||
Duration = eventData.Duration, | ||
Timestamp = DateTimeOffset.UtcNow | ||
}; | ||
try | ||
{ | ||
// TODO: cannot audit seeding/migrations data | ||
eventData.Context!.Add(log); | ||
eventData.Context.SaveChanges(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.ToString()); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Shiny.Extensions.EntityFramework/QueryLog/QueryLogEntry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Shiny.QueryLog; | ||
|
||
public class QueryLogEntry | ||
{ | ||
public int Id { get; set; } | ||
public string Query { get; set; } | ||
public TimeSpan Duration { get; set; } | ||
public DateTimeOffset Timestamp { get; set; } | ||
|
||
public string? UserIdentifier { get; set; } | ||
public string? AppLocation { get; set; } | ||
public string? UserIpAddress { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/Shiny.Extensions.EntityFramework.Tests/EfTests.QueryLogs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Shiny.Extensions.EntityFramework.Tests; | ||
|
||
public partial class EfTests | ||
{ | ||
|
||
[Fact] | ||
public async Task DidLog() | ||
{ | ||
await this.DoDb(async data => | ||
{ | ||
await data.Manufacturers.ToListAsync(); | ||
}); | ||
|
||
await this.DoDb(async data => | ||
{ | ||
var logs = await data.QueryLogs.ToListAsync(); | ||
logs.Count.Should().BeGreaterOrEqualTo(1, "No query logs"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Npgsql; | ||
|
||
namespace Shiny.Extensions.EntityFramework.Tests; | ||
|
||
|
||
public partial class EfTests : IDisposable | ||
{ | ||
readonly TestContextInfoProvider contextProvider; | ||
readonly IServiceProvider serviceProvider; | ||
|
||
public EfTests() | ||
{ | ||
this.contextProvider = new(); | ||
|
||
var services = new ServiceCollection(); | ||
services.AddDbContextAuditing(this.contextProvider); | ||
services.AddDbContextQueryLogging(this.contextProvider, TimeSpan.Zero); | ||
|
||
services.AddDbContext<TestDbContext>((sp, opts) => opts | ||
.UseNpgsql(new NpgsqlDataSourceBuilder("User ID=sa;Password=Blargh911!;Host=localhost;Port=5432;Database=AuditUnitTests;Pooling=true;Connection Lifetime=30;").Build()) | ||
.UseAuditing(sp) | ||
.UseQueryLogging(sp) | ||
// .UseSqlite("Data Source=test.db") | ||
); | ||
this.serviceProvider = services.BuildServiceProvider(); | ||
|
||
using var scope = this.serviceProvider.CreateScope(); | ||
var data = scope.ServiceProvider.GetRequiredService<TestDbContext>(); | ||
data.Database.EnsureDeleted(); | ||
data.Database.EnsureCreated(); | ||
|
||
// TODO: seed | ||
} | ||
|
||
async Task DoDb(Func<TestDbContext, Task> task) | ||
{ | ||
using var scope = this.serviceProvider.CreateScope(); | ||
var data = scope.ServiceProvider.GetRequiredService<TestDbContext>(); | ||
await task(data); | ||
} | ||
|
||
|
||
public void Dispose() => (this.serviceProvider as IDisposable)?.Dispose(); | ||
} | ||
|
||
public class TestContextInfoProvider : IContextInfoProvider | ||
{ | ||
public string? AppLocation { get; set; } = "UNIT TESTS"; | ||
public string? Tenant { get; set; } = "Test Tenant"; | ||
public string? UserIdentifier { get; set; } = "Test User"; | ||
public string? UserIpAddress { get; set; } = "0.0.0.0"; | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/Shiny.Extensions.EntityFramework.Tests/TestDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters