Skip to content

Commit 9f316e8

Browse files
author
Jennifer Deigendesch
committed
Added logging
1 parent 73a5609 commit 9f316e8

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
<li>Managing Static Files in ASP.NET Core: https://www.youtube.com/watch?v=n3rL0ekYEOM</li>
66
<li>ASP.NET Web API Help Pages using Swagger: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger</li>
77
<li>Middleware und Dependency Injection: https://channel9.msdn.com/Series/Einfhrung-in-ASPNET-Core/02-Middleware-und-Dependency-Injection</li>
8+
<li>Logging in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging</li>
89
</ul>

src/ASPNETCoreExample/Controllers/FoodController.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Microsoft.AspNetCore.JsonPatch;
88
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
910

1011
using Models;
1112

@@ -18,12 +19,16 @@
1819
public class FoodController : Controller {
1920
private readonly IFoodRepository foodRepository;
2021

22+
private readonly ILogger logger;
23+
2124
/// <summary>
2225
/// Constructor
2326
/// </summary>
2427
/// <param name="foodRepository">Repository of <see cref="FoodItem"/>.</param>
25-
public FoodController(IFoodRepository foodRepository) {
28+
/// <param name="logger">Logger of this class.</param>
29+
public FoodController(IFoodRepository foodRepository, ILogger<FoodController> logger) {
2630
this.foodRepository = foodRepository;
31+
this.logger = logger;
2732
}
2833

2934
/// <summary>
@@ -34,7 +39,7 @@ public FoodController(IFoodRepository foodRepository) {
3439
[HttpGet]
3540
[ProducesResponseType(typeof(IEnumerable<FoodDto>), 200)]
3641
public IActionResult GetAllFoodItems() {
37-
ICollection<FoodItem> foodItems = this.foodRepository.GetAll();
42+
IEnumerable<FoodItem> foodItems = this.foodRepository.GetAll();
3843
IEnumerable<FoodDto> mappedItems = foodItems.Select(AutoMapper.Mapper.Map<FoodDto>);
3944

4045
return this.Ok(mappedItems);
@@ -54,6 +59,7 @@ public IActionResult GetSingleFoodItem(int id) {
5459
FoodItem foodItem = this.foodRepository.GetSingle(id);
5560

5661
if (foodItem == null) {
62+
this.logger.LogWarning("Item with {ID} not found", id);
5763
return this.NotFound();
5864
}
5965

src/ASPNETCoreExample/Repositories/FoodRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public FoodItem Update(int id, FoodItem item) {
6666
/// Get all available <see cref="FoodItem"/>.
6767
/// </summary>
6868
/// <returns>Collection of <see cref="FoodItem"/>.</returns>
69-
public ICollection<FoodItem> GetAll() {
69+
public IEnumerable<FoodItem> GetAll() {
7070
return this.storage.Values;
7171
}
7272

src/ASPNETCoreExample/Repositories/IFoodRepository.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,48 @@
33

44
using Models;
55

6+
/// <summary>
7+
/// Interface of Repository for <see cref="FoodItem"/>.
8+
/// </summary>
69
public interface IFoodRepository {
10+
/// <summary>
11+
/// Get a single <see cref="FoodItem"/> with given <paramref name="id"/>.
12+
/// </summary>
13+
/// <param name="id">Id of the <see cref="FoodItem"/> to get.</param>
14+
/// <returns><see cref="FoodItem"/> with given <paramref name="id"/></returns>
715
FoodItem GetSingle(int id);
816

17+
/// <summary>
18+
/// Add given <see cref="FoodItem"/>.
19+
/// </summary>
20+
/// <param name="item"><see cref="FoodItem"/> to add.</param>
21+
/// <returns>Added <see cref="FoodItem"/>.</returns>
922
FoodItem Add(FoodItem item);
1023

24+
/// <summary>
25+
/// Delete <see cref="FoodItem"/> with given <paramref name="id"/>.
26+
/// </summary>
27+
/// <param name="id">Id of the <see cref="FoodItem"/> to delete.</param>
1128
void Delete(int id);
1229

30+
/// <summary>
31+
/// Update the values of given <paramref name="item"/> for <see cref="FoodItem"/> with given <paramref name="id"/>.
32+
/// </summary>
33+
/// <param name="id">Id of the <see cref="FoodItem"/> to update.</param>
34+
/// <param name="item">New values of the <see cref="FoodItem"/> to update.</param>
35+
/// <returns>Updated <see cref="FoodItem"/>.</returns>
1336
FoodItem Update(int id, FoodItem item);
1437

15-
ICollection<FoodItem> GetAll();
38+
/// <summary>
39+
/// Get all available <see cref="FoodItem"/>.
40+
/// </summary>
41+
/// <returns>Collection of <see cref="FoodItem"/>.</returns>
42+
IEnumerable<FoodItem> GetAll();
1643

44+
/// <summary>
45+
/// Count of <see cref="FoodItem"/> available within the storage.
46+
/// </summary>
47+
/// <returns>Count of <see cref="FoodItem"/> available within the storage.</returns>
1748
int Count();
1849
}
1950
}

src/ASPNETCoreExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void ConfigureServices(IServiceCollection services) {
7171
/// <param name="env"></param>
7272
/// <param name="loggerFactory"></param>
7373
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
74-
loggerFactory.AddConsole();
74+
loggerFactory.AddConsole(this.Configuration.GetSection("Logging")).AddDebug();
7575

7676
if (env.IsDevelopment()) {
7777
// Show info about an exception if one occurs only in development environment

src/ASPNETCoreExample/appsettings.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@
44
"description": "This is an example of a REST API with ASP.NET Core v1.0",
55
"contact-name": "Jennifer Deigendesch",
66
"contact-email": "[email protected]",
7-
"contact-url": "https://github.com/doubleSlashde/ASPNETCoreExample"
7+
"contact-url": "https://github.com/doubleSlashde/ASPNETCoreExample",
8+
9+
"Logging": {
10+
"IncludeScopes": false,
11+
"LogLevel": {
12+
"Default": "Debug",
13+
"System": "Debug",
14+
"Microsoft": "Debug"
15+
}
16+
}
817
}

src/ASPNETCoreExample/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
1616
"Swashbuckle": "6.0.0-beta902",
1717
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
18-
"Microsoft.Extensions.Configuration.Json": "1.0.0"
18+
"Microsoft.Extensions.Configuration.Json": "1.0.0",
19+
"Microsoft.Extensions.Logging.Debug": "1.0.0"
1920
},
2021

2122
"tools": {

0 commit comments

Comments
 (0)