Skip to content

Commit e05e7c5

Browse files
GarvinGarvin
Garvin
authored and
Garvin
committed
Adding logger to validate sql generated in tests
1 parent ba00b60 commit e05e7c5

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.IO;
4+
5+
namespace DataTablesParser.Tests
6+
{
7+
public class EFlogger : ILoggerProvider
8+
{
9+
public ILogger CreateLogger(string categoryName)
10+
{
11+
return new MyLogger();
12+
}
13+
14+
public void Dispose()
15+
{ }
16+
17+
private class MyLogger : ILogger
18+
{
19+
public bool IsEnabled(LogLevel logLevel)
20+
{
21+
return true;
22+
}
23+
24+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
25+
{
26+
27+
Console.WriteLine(formatter(state, exception));
28+
}
29+
30+
public IDisposable BeginScope<TState>(TState state)
31+
{
32+
return null;
33+
}
34+
}
35+
}
36+
}

test/DatatablesParser.Tests/TestHelper.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.EntityFrameworkCore;
88
using Pomelo.EntityFrameworkCore.MySql;
99
using Npgsql.EntityFrameworkCore.PostgreSQL;
10+
using Microsoft.Extensions.Logging;
1011

1112
namespace DataTablesParser.Tests
1213
{
@@ -167,15 +168,19 @@ public static PersonContext GetInMemoryContext()
167168

168169
public static PersonContext GetMysqlContext()
169170
{
170-
171+
171172
var serviceProvider = new ServiceCollection()
172173
.AddEntityFrameworkMySql()
173174
.BuildServiceProvider();
174-
175+
176+
177+
var lf = serviceProvider.GetService<ILoggerFactory>();
178+
lf.AddProvider(new EFlogger());
179+
175180
var builder = new DbContextOptionsBuilder<PersonContext>();
176181
builder.UseMySql(@"server=mysql;database=dotnettest;user=tester;password=Rea11ytrong_3")
177182
.UseInternalServiceProvider(serviceProvider);
178-
183+
179184
var context = new PersonContext(builder.Options);
180185

181186
context.Database.EnsureCreated();

0 commit comments

Comments
 (0)