Skip to content

Commit fe291bc

Browse files
author
Brian Cummings
committed
Added more comprehensive logging
1 parent fd10abe commit fe291bc

File tree

12 files changed

+454
-159
lines changed

12 files changed

+454
-159
lines changed

PathfinderHonorManager/Controllers/ClubController.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.AspNetCore.Mvc;
88
using PathfinderHonorManager.Model;
99
using PathfinderHonorManager.Service.Interfaces;
10+
using Microsoft.Extensions.Logging;
11+
using System.Linq;
1012

1113
namespace PathfinderHonorManager.Controllers
1214
{
@@ -20,10 +22,12 @@ namespace PathfinderHonorManager.Controllers
2022
public class ClubsController : ControllerBase
2123
{
2224
private readonly IClubService _clubService;
25+
private readonly ILogger<ClubsController> _logger;
2326

24-
public ClubsController(IClubService clubService)
27+
public ClubsController(IClubService clubService, ILogger<ClubsController> logger)
2528
{
2629
_clubService = clubService;
30+
_logger = logger;
2731
}
2832

2933
// GET Clubs
@@ -39,24 +43,30 @@ public async Task<ActionResult<IEnumerable<Club>>> GetClubs(CancellationToken to
3943
{
4044
if (clubcode == null)
4145
{
46+
_logger.LogInformation("Getting all clubs");
4247
var clubs = await _clubService.GetAllAsync(token);
4348

4449
if (clubs == default)
4550
{
51+
_logger.LogWarning("No clubs found");
4652
return NotFound();
4753
}
4854

55+
_logger.LogInformation("Retrieved {Count} clubs", clubs.Count());
4956
return Ok(clubs);
5057
}
5158
else
5259
{
60+
_logger.LogInformation("Getting club with code {ClubCode}", clubcode);
5361
var club = await _clubService.GetByCodeAsync(clubcode.ToUpper(), token);
5462

5563
if (club == default)
5664
{
65+
_logger.LogWarning("Club with code {ClubCode} not found", clubcode);
5766
return NotFound();
5867
}
5968

69+
_logger.LogInformation("Retrieved club with code {ClubCode}", clubcode);
6070
return Ok(club);
6171
}
6272
}
@@ -73,13 +83,16 @@ public async Task<ActionResult<IEnumerable<Club>>> GetClubs(CancellationToken to
7383
[HttpGet("{id:guid}")]
7484
public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
7585
{
86+
_logger.LogInformation("Getting club with ID {ClubId}", id);
7687
var club = await _clubService.GetByIdAsync(id, token);
7788

7889
if (club == default)
7990
{
91+
_logger.LogWarning("Club with ID {ClubId} not found", id);
8092
return NotFound();
8193
}
8294

95+
_logger.LogInformation("Retrieved club with ID {ClubId}", id);
8396
return Ok(club);
8497
}
8598
}

PathfinderHonorManager/Controllers/HonorsController.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using PathfinderHonorManager.Service.Interfaces;
1010
using Incoming = PathfinderHonorManager.Dto.Incoming;
1111
using Microsoft.AspNetCore.Authorization;
12+
using Microsoft.Extensions.Logging;
13+
using System.Linq;
1214

1315
namespace PathfinderHonorManager.Controllers
1416
{
@@ -22,10 +24,12 @@ namespace PathfinderHonorManager.Controllers
2224
public class HonorsController : CustomApiController
2325
{
2426
private readonly IHonorService _honorService;
27+
private readonly ILogger<HonorsController> _logger;
2528

26-
public HonorsController(IHonorService honorService)
29+
public HonorsController(IHonorService honorService, ILogger<HonorsController> logger)
2730
{
2831
_honorService = honorService;
32+
_logger = logger;
2933
}
3034

3135
// GET Honors
@@ -38,13 +42,16 @@ public HonorsController(IHonorService honorService)
3842
[HttpGet]
3943
public async Task<ActionResult<IEnumerable<Honor>>> GetHonors(CancellationToken token)
4044
{
45+
_logger.LogInformation("Getting all honors");
4146
var honors = await this._honorService.GetAllAsync(token);
4247

4348
if (honors == default)
4449
{
50+
_logger.LogWarning("No honors found");
4551
return NotFound();
4652
}
4753

54+
_logger.LogInformation("Retrieved {Count} honors", honors.Count());
4855
return Ok(honors);
4956
}
5057

@@ -60,13 +67,16 @@ public async Task<ActionResult<IEnumerable<Honor>>> GetHonors(CancellationToken
6067
[HttpGet("{id:guid}")]
6168
public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
6269
{
70+
_logger.LogInformation("Getting honor with ID {HonorId}", id);
6371
var honor = await this._honorService.GetByIdAsync(id, token);
6472

6573
if (honor == default)
6674
{
75+
_logger.LogWarning("Honor with ID {HonorId} not found", id);
6776
return NotFound();
6877
}
6978

79+
_logger.LogInformation("Retrieved honor with ID {HonorId}", id);
7080
return Ok(new { id = honor.HonorID, honor });
7181
}
7282

@@ -83,21 +93,25 @@ public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
8393
[HttpPost]
8494
public async Task<ActionResult<Honor>> Post([FromBody] Incoming.HonorDto newHonor, CancellationToken token)
8595
{
96+
_logger.LogInformation("Creating new honor");
8697
try
8798
{
8899
var honor = await _honorService.AddAsync(newHonor, token);
89100

101+
_logger.LogInformation("Created honor with ID {HonorId}", honor.HonorID);
90102
return CreatedAtRoute(
91103
routeValues: GetByIdAsync(honor.HonorID, token),
92104
honor);
93105
}
94106
catch (FluentValidation.ValidationException ex)
95107
{
108+
_logger.LogWarning(ex, "Validation failed while creating honor");
96109
UpdateModelState(ex);
97110
return ValidationProblem(ModelState);
98111
}
99112
catch (DbUpdateException ex)
100113
{
114+
_logger.LogError(ex, "Database error while creating honor");
101115
return ValidationProblem(ex.Message);
102116
}
103117
}
@@ -116,20 +130,37 @@ public async Task<ActionResult<Honor>> Post([FromBody] Incoming.HonorDto newHono
116130
[HttpPut("{id:guid}")]
117131
public async Task<IActionResult> Put(Guid id, [FromBody] Incoming.HonorDto updatedHonor, CancellationToken token)
118132
{
133+
_logger.LogInformation("Updating honor with ID {HonorId}", id);
119134
var honor = await _honorService.GetByIdAsync(id, token);
120135

121136
if (honor == default)
122137
{
138+
_logger.LogWarning("Honor with ID {HonorId} not found", id);
123139
return NotFound();
124140
}
125141

126-
await _honorService.UpdateAsync(id, updatedHonor, token);
142+
try
143+
{
144+
await _honorService.UpdateAsync(id, updatedHonor, token);
127145

128-
honor = await _honorService.GetByIdAsync(id, token);
146+
honor = await _honorService.GetByIdAsync(id, token);
147+
_logger.LogInformation("Updated honor with ID {HonorId}", id);
129148

130-
return honor != default
131-
? Ok(honor)
132-
: NotFound();
149+
return honor != default
150+
? Ok(honor)
151+
: NotFound();
152+
}
153+
catch (FluentValidation.ValidationException ex)
154+
{
155+
_logger.LogWarning(ex, "Validation failed while updating honor with ID {HonorId}", id);
156+
UpdateModelState(ex);
157+
return ValidationProblem(ModelState);
158+
}
159+
catch (DbUpdateException ex)
160+
{
161+
_logger.LogError(ex, "Database error while updating honor with ID {HonorId}", id);
162+
return ValidationProblem(ex.Message);
163+
}
133164
}
134165
}
135166
}

PathfinderHonorManager/Controllers/PathfinderController.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Authorization;
1212
using System.Linq;
1313
using Microsoft.AspNetCore.Routing;
14+
using Microsoft.Extensions.Logging;
1415

1516
namespace PathfinderHonorManager.Controllers
1617
{
@@ -24,16 +25,18 @@ namespace PathfinderHonorManager.Controllers
2425
public class PathfindersController : CustomApiController
2526
{
2627
private readonly IPathfinderService _pathfinderService;
28+
private readonly ILogger<PathfindersController> _logger;
2729

2830
private string GetClubCodeFromContext()
2931
{
3032
var clubCode = HttpContext.User.FindFirst("clubCode")?.Value;
3133
return clubCode;
3234
}
3335

34-
public PathfindersController(IPathfinderService pathfinderService)
36+
public PathfindersController(IPathfinderService pathfinderService, ILogger<PathfindersController> logger)
3537
{
3638
_pathfinderService = pathfinderService;
39+
_logger = logger;
3740
}
3841

3942
// GET Pathfinders
@@ -48,13 +51,17 @@ public PathfindersController(IPathfinderService pathfinderService)
4851
public async Task<ActionResult<IEnumerable<Outgoing.PathfinderDependantDto>>> GetAll(CancellationToken token, bool showInactive = false)
4952
{
5053
var clubCode = GetClubCodeFromContext();
54+
_logger.LogInformation("Getting all pathfinders for club {ClubCode}, showInactive: {ShowInactive}", clubCode, showInactive);
55+
5156
var pathfinders = await _pathfinderService.GetAllAsync(clubCode, showInactive, token);
5257

5358
if (pathfinders == null || !pathfinders.Any())
5459
{
60+
_logger.LogWarning("No pathfinders found for club {ClubCode}", clubCode);
5561
return NotFound();
5662
}
5763

64+
_logger.LogInformation("Retrieved {Count} pathfinders for club {ClubCode}", pathfinders.Count(), clubCode);
5865
return Ok(pathfinders);
5966
}
6067

@@ -71,13 +78,17 @@ public PathfindersController(IPathfinderService pathfinderService)
7178
public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
7279
{
7380
var clubCode = GetClubCodeFromContext();
81+
_logger.LogInformation("Getting pathfinder with ID {PathfinderId} for club {ClubCode}", id, clubCode);
82+
7483
var pathfinder = await _pathfinderService.GetByIdAsync(id, clubCode, token);
7584

7685
if (pathfinder == default)
7786
{
87+
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found for club {ClubCode}", id, clubCode);
7888
return NotFound();
7989
}
8090

91+
_logger.LogInformation("Retrieved pathfinder with ID {PathfinderId} for club {ClubCode}", id, clubCode);
8192
return Ok(pathfinder);
8293
}
8394

@@ -95,24 +106,28 @@ public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
95106
public async Task<IActionResult> PostAsync([FromBody] Incoming.PathfinderDto newPathfinder, CancellationToken token)
96107
{
97108
var clubCode = GetClubCodeFromContext();
109+
_logger.LogInformation("Creating new pathfinder for club {ClubCode}", clubCode);
110+
98111
try
99112
{
100113
var pathfinder = await _pathfinderService.AddAsync(newPathfinder, clubCode, token);
101114

115+
_logger.LogInformation("Created pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinder.PathfinderID, clubCode);
102116
return CreatedAtRoute(
103117
routeValues: GetByIdAsync(pathfinder.PathfinderID, token),
104118
pathfinder);
105119
}
106120
catch (FluentValidation.ValidationException ex)
107121
{
122+
_logger.LogWarning(ex, "Validation failed while creating pathfinder for club {ClubCode}", clubCode);
108123
UpdateModelState(ex);
109124
return ValidationProblem(ModelState);
110125
}
111126
catch (DbUpdateException ex)
112127
{
128+
_logger.LogError(ex, "Database error while creating pathfinder for club {ClubCode}", clubCode);
113129
return ValidationProblem(ex.Message);
114130
}
115-
116131
}
117132

118133
// PUT Pathfinders/{pathfinderId}
@@ -130,21 +145,30 @@ public async Task<IActionResult> PostAsync([FromBody] Incoming.PathfinderDto new
130145
public async Task<IActionResult> PutAsync(Guid pathfinderId, [FromBody] Incoming.PutPathfinderDto updatedPathfinder, CancellationToken token)
131146
{
132147
var clubCode = GetClubCodeFromContext();
148+
_logger.LogInformation("Updating pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
149+
133150
try
134151
{
135152
var pathfinder = await _pathfinderService.UpdateAsync(pathfinderId, updatedPathfinder, clubCode, token);
136153

137-
return pathfinder != default
138-
? Ok(pathfinder)
139-
: NotFound();
154+
if (pathfinder == default)
155+
{
156+
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found for club {ClubCode}", pathfinderId, clubCode);
157+
return NotFound();
158+
}
159+
160+
_logger.LogInformation("Updated pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
161+
return Ok(pathfinder);
140162
}
141163
catch (FluentValidation.ValidationException ex)
142164
{
165+
_logger.LogWarning(ex, "Validation failed while updating pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
143166
UpdateModelState(ex);
144167
return ValidationProblem(ModelState);
145168
}
146169
catch (DbUpdateException ex)
147170
{
171+
_logger.LogError(ex, "Database error while updating pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
148172
return ValidationProblem(ex.Message);
149173
}
150174
}
@@ -163,6 +187,8 @@ public async Task<IActionResult> PutAsync(Guid pathfinderId, [FromBody] Incoming
163187
public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<Incoming.BulkPutPathfinderDto> bulkData, CancellationToken token)
164188
{
165189
var clubCode = GetClubCodeFromContext();
190+
_logger.LogInformation("Bulk updating {Count} pathfinders for club {ClubCode}", bulkData.Count(), clubCode);
191+
166192
var responses = new List<object>();
167193

168194
foreach (var data in bulkData)
@@ -178,9 +204,19 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
178204
status = pathfinder != null ? StatusCodes.Status200OK : StatusCodes.Status404NotFound,
179205
pathfinderId = item.PathfinderId,
180206
});
207+
208+
if (pathfinder == null)
209+
{
210+
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found during bulk update for club {ClubCode}", item.PathfinderId, clubCode);
211+
}
212+
else
213+
{
214+
_logger.LogInformation("Updated pathfinder with ID {PathfinderId} during bulk update for club {ClubCode}", item.PathfinderId, clubCode);
215+
}
181216
}
182217
catch (FluentValidation.ValidationException ex)
183218
{
219+
_logger.LogWarning(ex, "Validation failed while bulk updating pathfinder with ID {PathfinderId} for club {ClubCode}", item.PathfinderId, clubCode);
184220
responses.Add(new
185221
{
186222
status = StatusCodes.Status400BadRequest,
@@ -190,6 +226,7 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
190226
}
191227
catch (DbUpdateException ex)
192228
{
229+
_logger.LogError(ex, "Database error while bulk updating pathfinder with ID {PathfinderId} for club {ClubCode}", item.PathfinderId, clubCode);
193230
responses.Add(new
194231
{
195232
status = StatusCodes.Status400BadRequest,
@@ -200,6 +237,7 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
200237
}
201238
}
202239

240+
_logger.LogInformation("Completed bulk update of {Count} pathfinders for club {ClubCode}", bulkData.Count(), clubCode);
203241
return StatusCode(StatusCodes.Status207MultiStatus, responses);
204242
}
205243
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace PathfinderHonorManager.Dto.Incoming
5+
{
6+
public class ClubDto
7+
{
8+
[Required]
9+
public string Name { get; set; }
10+
11+
[Required]
12+
public string ClubCode { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)