Skip to content

Commit 882d295

Browse files
updates for levels/categories/filters
1 parent 0a58b84 commit 882d295

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"env": {
2121
"ASPNETCORE_ENVIRONMENT": "Development"
22+
//"Logging__LogLevel__CarvedRock.Api.Controllers": "Debug"
2223
},
2324
"sourceFileMap": {
2425
"/Views": "${workspaceFolder}/Views"

CarvedRock.Api/Controllers/ProductController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public async Task<IEnumerable<Product>> Get(string category = "all")
3131
public async Task<IActionResult> Get(int id)
3232
{
3333
//var product = await _productLogic.GetProductByIdAsync(id);
34+
_logger.LogDebug("Getting single product in API for {id}", id);
3435
var product = _productLogic.GetProductById(id);
3536
if (product != null)
3637
{
3738
return Ok(product);
3839
}
40+
_logger.LogWarning("No product found for ID: {id}", id);
3941
return NotFound();
4042
}
4143
}

CarvedRock.Api/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
using System.Diagnostics;
12
using CarvedRock.Data;
23
using CarvedRock.Domain;
34

45
var builder = WebApplication.CreateBuilder(args);
6+
//builder.Logging.AddFilter("CarvedRock", LogLevel.Debug);
7+
8+
// var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
9+
// var tracePath = Path.Join(path, $"Log_CarvedRock_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt");
10+
// Trace.Listeners.Add(new TextWriterTraceListener(System.IO.File.CreateText(tracePath)));
11+
// Trace.AutoFlush = true;
512

613
// Services
714
builder.Services.AddControllers();

CarvedRock.Api/appsettings.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Information",
4+
"Default": "Information",
55
"Microsoft.AspNetCore": "Warning"
66
}
7+
// "Console": {
8+
// "LogLevel": {
9+
// "Default": "Information",
10+
// "CarvedRock.Domain": "Debug",
11+
// "Microsoft.AspNetCore": "Warning"
12+
// },
13+
// "FormatterName": "json",
14+
// "FormatterOptions": {
15+
// "SingleLine": true,
16+
// "IncludeScopes": true,
17+
// "TimestampFormat": "HH:mm:ss ",
18+
// "UseUtcTimestamp": true,
19+
// "JsonWriterOptions": {
20+
// "Indented": true
21+
// }
22+
// }
23+
// },
24+
// "Debug": {
25+
// "LogLevel": {
26+
// "Default": "Debug",
27+
// "Microsoft.AspNetCore": "Information"
28+
// }
29+
// }
730
},
831
"AllowedHosts": "*"
932
}

CarvedRock.Data/CarvedRock.Data.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
</PackageReference>
1414
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
15+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
1516
</ItemGroup>
1617

1718
</Project>

CarvedRock.Data/CarvedRockRepository.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
using CarvedRock.Data.Entities;
1+
using System.Diagnostics;
2+
using CarvedRock.Data.Entities;
23
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Logging;
35

46
namespace 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
}

CarvedRock.Domain/ProductLogic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public IEnumerable<Product> GetProductsForCategory(string category)
3333

3434
public Product? GetProductById(int id)
3535
{
36+
_logger.LogDebug("Logic for single product ({id})", id);
3637
return _repo.GetProductById(id);
3738
}
3839
}

0 commit comments

Comments
 (0)