Skip to content

Commit d53e0db

Browse files
authored
Release v0.7.0
* Adds Log Project in infra Layer * Adds Log to Application Layer * Adds Log to Presentation Layer * Adds Log Middleware to API Request/Response * Adds gitignore for entropy.bin * Changes Development appsettings to use Trace config
1 parent a2d402a commit d53e0db

40 files changed

Lines changed: 667 additions & 184 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,6 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
# temp database entropy.bin
353+
Entropy.bin
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Text.Json;
2+
3+
namespace FinancialHub.Common.Extensions
4+
{
5+
public static class ObjectExtensions
6+
{
7+
public static string ToJson(this object obj)
8+
{
9+
return JsonSerializer.Serialize(
10+
obj,
11+
new JsonSerializerOptions()
12+
{
13+
PropertyNameCaseInsensitive = true,
14+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
15+
}
16+
);
17+
}
18+
19+
public static T? FromJson<T>(this string json)
20+
{
21+
return JsonSerializer.Deserialize<T>(
22+
json,
23+
new JsonSerializerOptions()
24+
{
25+
PropertyNameCaseInsensitive = true,
26+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
27+
}
28+
);
29+
}
30+
}
31+
}

src/api/core/FinancialHub.Core.Application/FinancialHub.Core.Application.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
1111
<PackageReference Include="FluentValidation" Version="11.0.0" />
1212
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>
Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using AutoMapper;
22
using FinancialHub.Core.Domain.DTOS.Accounts;
3+
using Microsoft.Extensions.Logging;
34
using FinancialHub.Core.Domain.Interfaces.Validators;
5+
using FinancialHub.Common.Extensions;
46

57
namespace FinancialHub.Core.Application.Services
68
{
@@ -9,76 +11,112 @@ public class AccountsService : IAccountsService
911
private readonly IAccountsProvider provider;
1012
private readonly IAccountsValidator accountValidator;
1113
private readonly IMapper mapper;
14+
private readonly ILogger<AccountsService> logger;
1215

1316
public AccountsService(
1417
IAccountsProvider provider, IAccountsValidator accountValidator,
15-
IMapper mapper
18+
IMapper mapper, ILogger<AccountsService> logger
1619
)
1720
{
1821
this.provider = provider;
1922
this.accountValidator = accountValidator;
2023
this.mapper = mapper;
24+
this.logger = logger;
2125
}
2226

2327
public async Task<ServiceResult<AccountDto>> CreateAsync(CreateAccountDto accountDto)
2428
{
29+
this.logger.LogInformation("Creating account {name}", accountDto.Name);
30+
this.logger.LogTrace("Account data : {accountDto}", accountDto.ToJson());
31+
2532
var validationResult = await this.accountValidator.ValidateAsync(accountDto);
2633
if(validationResult.HasError)
2734
{
35+
this.logger.LogTrace("Account creation Validation result : {validationResult}", validationResult.ToJson());
36+
this.logger.LogInformation("Failed creating account {name}", accountDto.Name);
2837
return validationResult.Error;
2938
}
3039

3140
var account = this.mapper.Map<AccountModel>(accountDto);
3241

33-
var result = await this.provider.CreateAsync(account);
42+
var accountModel = await this.provider.CreateAsync(account);
43+
44+
var result = this.mapper.Map<AccountDto>(accountModel);
3445

35-
return this.mapper.Map<AccountDto>(result);
46+
this.logger.LogTrace("Account creation result : {result}", result.ToJson());
47+
this.logger.LogInformation("Account {name} Sucessfully created", result.Name);
48+
49+
return result;
3650
}
3751

3852
public async Task<ServiceResult<int>> DeleteAsync(Guid id)
3953
{
40-
return await this.provider.DeleteAsync(id);
54+
this.logger.LogInformation("Removing account {id}", id);
55+
var amount = await this.provider.DeleteAsync(id);
56+
this.logger.LogInformation("Account {id} {removed}", id, amount > 0 ? "removed" : "not removed");
57+
return amount;
4158
}
4259

4360
public async Task<ServiceResult<ICollection<AccountDto>>> GetAllAsync()
4461
{
62+
this.logger.LogInformation("Getting all accounts");
4563
var accounts = await this.provider.GetAllAsync();
4664

65+
this.logger.LogInformation("Returning {count} accounts", accounts.Count > 0? $"{accounts.Count}": "no");
4766
return this.mapper.Map<ICollection<AccountDto>>(accounts).ToArray();
4867
}
4968

5069
public async Task<ServiceResult<AccountDto>> GetByIdAsync(Guid id)
5170
{
52-
var accountExists = await this.accountValidator.ExistsAsync(id);
53-
if (accountExists.HasError)
71+
this.logger.LogInformation("Getting account {id}", id);
72+
73+
var validationResult = await this.accountValidator.ExistsAsync(id);
74+
if (validationResult.HasError)
5475
{
55-
return accountExists.Error;
76+
this.logger.LogTrace("Account get by id result : {validationResult}", validationResult.ToJson());
77+
this.logger.LogInformation("Failed getting account {id}", id);
78+
return validationResult.Error;
5679
}
5780

5881
var existingAccount = await this.provider.GetByIdAsync(id);
5982

60-
return this.mapper.Map<AccountDto>(existingAccount);
83+
var account = this.mapper.Map<AccountDto>(existingAccount);
84+
85+
this.logger.LogTrace("Account result {account}", account.ToJson());
86+
this.logger.LogInformation("Account {id} found", id);
87+
return account;
6188
}
6289

6390
public async Task<ServiceResult<AccountDto>> UpdateAsync(Guid id, UpdateAccountDto account)
6491
{
92+
this.logger.LogInformation("Updating account {id}", id);
93+
6594
var validationResult = await this.accountValidator.ValidateAsync(account);
6695
if (validationResult.HasError)
6796
{
97+
this.logger.LogTrace("Account update validation result : {validationResult}", validationResult.ToJson());
98+
this.logger.LogInformation("Failed updating account {id}", id);
6899
return validationResult.Error;
69100
}
70101

71102
validationResult = await this.accountValidator.ExistsAsync(id);
72103
if (validationResult.HasError)
73104
{
105+
this.logger.LogTrace("Account update validation result : {validationResult}", validationResult.ToJson());
106+
this.logger.LogInformation("Failed updating account {id}", id);
74107
return validationResult.Error;
75108
}
76109

77110
var accountModel = this.mapper.Map<AccountModel>(account);
78111

79112
var updatedAccount = await this.provider.UpdateAsync(id, accountModel);
80113

81-
return this.mapper.Map<AccountDto>(updatedAccount);
114+
var result = this.mapper.Map<AccountDto>(updatedAccount);
115+
116+
this.logger.LogTrace("Account update result : {result}", result.ToJson());
117+
this.logger.LogInformation("Account {id} Sucessfully Updated", id);
118+
119+
return result;
82120
}
83121
}
84122
}
Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using AutoMapper;
2+
using FinancialHub.Common.Extensions;
23
using FinancialHub.Core.Domain.DTOS.Balances;
34
using FinancialHub.Core.Domain.Interfaces.Validators;
5+
using Microsoft.Extensions.Logging;
46

57
namespace FinancialHub.Core.Application.Services
68
{
@@ -9,98 +11,144 @@ public class BalancesService : IBalancesService
911
private readonly IBalancesProvider balancesProvider;
1012
private readonly IBalancesValidator balancesValidator;
1113
private readonly IAccountsValidator accountsValidator;
14+
private readonly ILogger<BalancesService> logger;
1215
private readonly IMapper mapper;
1316

1417
public BalancesService(
1518
IBalancesProvider balancesProvider,
1619
IBalancesValidator balancesValidator, IAccountsValidator accountsValidator,
17-
IMapper mapper
20+
IMapper mapper, ILogger<BalancesService> logger
1821
)
1922
{
2023
this.balancesProvider = balancesProvider;
2124
this.balancesValidator = balancesValidator;
2225
this.accountsValidator = accountsValidator;
2326
this.mapper = mapper;
27+
this.logger = logger;
2428
}
2529

2630
public async Task<ServiceResult<BalanceDto>> CreateAsync(CreateBalanceDto balance)
2731
{
28-
var validationResult = await this.balancesValidator.ValidateAsync(balance);
29-
if (validationResult.HasError)
30-
return validationResult.Error;
32+
this.logger.LogInformation("Creating balance {name} in account {id}", balance.Name, balance.AccountId);
33+
this.logger.LogTrace("Balance data : {balance}", balance.ToJson());
3134

32-
validationResult = await this.accountsValidator.ExistsAsync(balance.AccountId);
35+
var validationResult = await this.balancesValidator.ValidateAsync(balance);
3336
if (validationResult.HasError)
37+
{
38+
this.logger.LogTrace("Balance creation Validation result : {validationResult}", validationResult.ToJson());
39+
this.logger.LogInformation("Failed creating account {name}", balance.Name);
3440
return validationResult.Error;
41+
}
3542

3643
var balanceModel = this.mapper.Map<BalanceModel>(balance);
3744

3845
var createdBalance = await this.balancesProvider.CreateAsync(balanceModel);
3946

40-
return this.mapper.Map<BalanceDto>(createdBalance);
47+
var result = this.mapper.Map<BalanceDto>(createdBalance);
48+
this.logger.LogTrace("Balance creation result : {result}", result.ToJson());
49+
this.logger.LogInformation("Balance {name} Sucessfully created in account {id}", result.Name, result.Account?.Id);
50+
return result;
4151
}
4252

4353
public async Task<ServiceResult<int>> DeleteAsync(Guid id)
4454
{
45-
return await this.balancesProvider.DeleteAsync(id);
55+
this.logger.LogInformation("Removing balance {id}", id);
56+
var amount = await this.balancesProvider.DeleteAsync(id);
57+
this.logger.LogInformation("Balance {id} {removed}", id, amount > 0 ? "removed" : "not removed");
58+
return amount;
4659
}
4760

4861
public async Task<ServiceResult<BalanceDto>> GetByIdAsync(Guid id)
4962
{
63+
this.logger.LogInformation("Getting balance {id}", id);
5064
var validationResult = await this.balancesValidator.ExistsAsync(id);
5165
if (validationResult.HasError)
5266
{
67+
this.logger.LogTrace("Balance get by id result : {validationResult}", validationResult);
68+
this.logger.LogInformation("Failed getting balance {id}", id);
5369
return validationResult.Error;
5470
}
5571

56-
var balance = await this.balancesProvider.GetByIdAsync(id);
72+
var existingBalance = await this.balancesProvider.GetByIdAsync(id);
73+
74+
var balance = this.mapper.Map<BalanceDto>(existingBalance);
5775

58-
return this.mapper.Map<BalanceDto>(balance);
76+
this.logger.LogTrace("Balance result {balance}", balance);
77+
this.logger.LogInformation("Balance {id} found", id);
78+
return balance;
5979
}
6080

6181
public async Task<ServiceResult<ICollection<BalanceDto>>> GetAllByAccountAsync(Guid accountId)
6282
{
83+
this.logger.LogInformation("Getting balances from account {accountId}", accountId);
6384
var validationResult = await this.accountsValidator.ExistsAsync(accountId);
6485
if (validationResult.HasError)
6586
{
87+
this.logger.LogTrace("Balances get by account result : {validationResult}", validationResult.ToJson());
88+
this.logger.LogInformation("Account {AccountId} not found", accountId);
6689
return validationResult.Error;
6790
}
6891

6992
var balances = await this.balancesProvider.GetAllByAccountAsync(accountId);
7093

71-
return this.mapper.Map<ICollection<BalanceDto>>(balances).ToArray();
94+
var result = this.mapper.Map<ICollection<BalanceDto>>(balances).ToArray();
95+
96+
this.logger.LogTrace("Balance result : {balances}", balances.ToJson());
97+
this.logger.LogInformation(
98+
"{amount} returned from account {accountId}",
99+
result.Length >0 ? $"{result.Length} balances" : "no balances",
100+
accountId
101+
);
102+
103+
return result;
72104
}
73105

74106
public async Task<ServiceResult<BalanceDto>> UpdateAsync(Guid id, UpdateBalanceDto balance)
75107
{
108+
this.logger.LogInformation("Updating balance {name} in account {id}", balance.Name, balance.AccountId);
109+
this.logger.LogTrace("Balance data : {balance}", balance.ToJson());
76110
var validationResult = await this.balancesValidator.ValidateAsync(balance);
77111
if (validationResult.HasError)
112+
{
113+
this.logger.LogTrace("Balance update validation result : {validationResult}", validationResult.ToJson());
114+
this.logger.LogInformation("Failed updating balance {id}", id);
78115
return validationResult.Error;
116+
}
79117

80118
validationResult = await this.balancesValidator.ExistsAsync(id);
81119
if (validationResult.HasError)
120+
{
121+
this.logger.LogTrace("Balance update validation result : {validationResult}", validationResult.ToJson());
122+
this.logger.LogInformation("Balance {id} not found", id);
82123
return validationResult.Error;
83-
84-
validationResult = await this.accountsValidator.ExistsAsync(balance.AccountId);
85-
if (validationResult.HasError)
86-
return validationResult.Error;
124+
}
87125

88126
var balanceModel = this.mapper.Map<BalanceModel>(balance);
89127

90128
var updatedBalance = await this.balancesProvider.UpdateAsync(id, balanceModel);
91129

92-
return this.mapper.Map<BalanceDto>(updatedBalance);
130+
var result = this.mapper.Map<BalanceDto>(updatedBalance);
131+
this.logger.LogTrace("Balance update result : {result}", result.ToJson());
132+
this.logger.LogInformation("Balance {name} Sucessfully created", result.Name);
133+
return result;
93134
}
94135

95136
public async Task<ServiceResult<BalanceModel>> UpdateAmountAsync(Guid id, decimal newAmount)
96137
{
97-
var oldBalance = await this.balancesValidator.ExistsAsync(id);
98-
if (oldBalance.HasError)
138+
this.logger.LogInformation("Getting balance {id}", id);
139+
var validationResult = await this.balancesValidator.ExistsAsync(id);
140+
if (validationResult.HasError)
99141
{
100-
return oldBalance.Error;
142+
this.logger.LogTrace("Balance get by id result : {validationResult}", validationResult.ToJson());
143+
this.logger.LogInformation("Failed getting balance {id}", id);
144+
return validationResult.Error;
101145
}
102146

103-
return await balancesProvider.UpdateAmountAsync(id, newAmount);
147+
var updatedBalance = await balancesProvider.UpdateAmountAsync(id, newAmount);
148+
149+
this.logger.LogTrace("Update Balance amount result : {updatedBalance}", updatedBalance.ToJson());
150+
this.logger.LogInformation("Update Balance amount {balanceId} in account {accountId}", updatedBalance.Id, updatedBalance.AccountId);
151+
return updatedBalance;
104152
}
105153
}
106154
}

0 commit comments

Comments
 (0)