diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-04 23:44:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-04 23:44:02 +0800 |
commit | b1fa31027706f7c0e31970089e8145dc70d48e6d (patch) | |
tree | 9016b0017634a7589981a38629f9b27ad76fb9ee /Timeline/Controllers/UserController.cs | |
parent | ebda3fc381ee4ed9f729fa85c1cee837ce4c5c3b (diff) | |
parent | 4eb1eb1a424b40adfa3bed79b9e58ce49c5a02c4 (diff) | |
download | timeline-b1fa31027706f7c0e31970089e8145dc70d48e6d.tar.gz timeline-b1fa31027706f7c0e31970089e8145dc70d48e6d.tar.bz2 timeline-b1fa31027706f7c0e31970089e8145dc70d48e6d.zip |
Merge pull request #36 from crupest/log
Improve log.
Diffstat (limited to 'Timeline/Controllers/UserController.cs')
-rw-r--r-- | Timeline/Controllers/UserController.cs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 84267520..2099690c 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -7,6 +7,7 @@ using Timeline.Authenticate; using Timeline.Entities; using Timeline.Entities.Http; using Timeline.Services; +using static Timeline.Helpers.MyLogHelper; namespace Timeline.Controllers { @@ -44,7 +45,7 @@ namespace Timeline.Controllers var user = await _userService.GetUser(username); if (user == null) { - _logger.LogInformation("Attempt to get a non-existent user. Username: {} .", username); + _logger.LogInformation(FormatLogMessage("Attempt to get a non-existent user.", Pair("Username", username))); return NotFound(new CommonResponse(ErrorCodes.Get_NotExists, "The user does not exist.")); } return Ok(user); @@ -53,7 +54,7 @@ namespace Timeline.Controllers [HttpPut("user/{username}"), AdminAuthorize] public async Task<IActionResult> Put([FromBody] UserPutRequest request, [FromRoute] string username) { - if (request.Password == null) + if (request.Password == null) // This place will be refactored. { _logger.LogInformation("Attempt to put a user without a password. Username: {} .", username); return BadRequest(); @@ -63,10 +64,10 @@ namespace Timeline.Controllers switch (result) { case PutResult.Created: - _logger.LogInformation("Created a user. Username: {} .", username); + _logger.LogInformation(FormatLogMessage("A user is created.", Pair("Username", username))); return CreatedAtAction("Get", new { username }, CommonPutResponse.Created); case PutResult.Modified: - _logger.LogInformation("Modified a user. Username: {} .", username); + _logger.LogInformation(FormatLogMessage("A user is modified.", Pair("Username", username))); return Ok(CommonPutResponse.Modified); default: throw new Exception("Unreachable code."); @@ -83,7 +84,7 @@ namespace Timeline.Controllers } catch (UserNotExistException e) { - _logger.LogInformation(e, "Attempt to patch a non-existent user. Username: {} .", username); + _logger.LogInformation(e, FormatLogMessage("Attempt to patch a non-existent user.", Pair("Username", username))); return BadRequest(new CommonResponse(ErrorCodes.Patch_NotExists, "The user does not exist.")); } } @@ -94,12 +95,12 @@ namespace Timeline.Controllers try { await _userService.DeleteUser(username); - _logger.LogInformation("A user is deleted. Username: {} .", username); + _logger.LogInformation(FormatLogMessage("A user is deleted.", Pair("Username", username))); return Ok(CommonDeleteResponse.Deleted); } catch (UserNotExistException e) { - _logger.LogInformation(e, "Attempt to delete a non-existent user. Username: {} .", username); + _logger.LogInformation(e, FormatLogMessage("Attempt to delete a non-existent user.", Pair("Username", username))); return Ok(CommonDeleteResponse.NotExists); } } @@ -110,12 +111,13 @@ namespace Timeline.Controllers try { await _userService.ChangePassword(User.Identity.Name, request.OldPassword, request.NewPassword); - _logger.LogInformation("A user changed password. Username: {} .", User.Identity.Name); + _logger.LogInformation(FormatLogMessage("A user changed password.", Pair("Username", User.Identity.Name))); return Ok(); } catch (BadPasswordException e) { - _logger.LogInformation(e, "A user attempt to change password but old password is wrong. Username: {} .", User.Identity.Name); + _logger.LogInformation(e, FormatLogMessage("A user attempt to change password but old password is wrong.",
+ Pair("Username", User.Identity.Name), Pair("Old Password", request.OldPassword))); return BadRequest(new CommonResponse(ErrorCodes.ChangePassword_BadOldPassword, "Old password is wrong.")); } // User can't be non-existent or the token is bad. |