1- using CarvedRock . Data . Entities ;
1+ using System . Diagnostics ;
2+ using CarvedRock . Data . Entities ;
23using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . Logging ;
35
46namespace CarvedRock . Data
57{
68 public class CarvedRockRepository : ICarvedRockRepository
79 {
810 private readonly LocalContext _ctx ;
11+ private readonly ILogger < CarvedRockRepository > _logger ;
12+ private readonly ILogger _factoryLogger ;
913
10- public CarvedRockRepository ( LocalContext ctx )
14+ public CarvedRockRepository ( LocalContext ctx , ILogger < CarvedRockRepository > logger ,
15+ ILoggerFactory loggerFactory )
1116 {
1217 _ctx = ctx ;
18+ _logger = logger ;
19+ _factoryLogger = loggerFactory . CreateLogger ( "DataAccessLayer" ) ;
1320 }
1421 public async Task < List < Product > > GetProductsAsync ( string category )
1522 {
23+ _logger . LogInformation ( "Getting products in repository for {category}" , category ) ;
1624 return await _ctx . Products . Where ( p => p . Category == category || category == "all" ) . ToListAsync ( ) ;
1725 }
1826
@@ -28,7 +36,18 @@ public List<Product> GetProducts(string category)
2836
2937 public Product ? GetProductById ( int id )
3038 {
31- return _ctx . Products . Find ( id ) ;
32- }
39+ var timer = new Stopwatch ( ) ;
40+ timer . Start ( ) ;
41+ var product = _ctx . Products . Find ( id ) ;
42+ timer . Stop ( ) ;
43+
44+ _logger . LogDebug ( "Querying products for {id} finished in {milliseconds} milliseconds" ,
45+ id , timer . ElapsedMilliseconds ) ;
46+
47+ _factoryLogger . LogInformation ( "(F) Querying products for {id} finished in {ticks} ticks" ,
48+ id , timer . ElapsedTicks ) ;
49+
50+ return product ;
51+ }
3352 }
3453}
0 commit comments