---
# System prepended metadata

title: Untitled

---

The detailed guidelines could be found at here: Section 4.11 [DD160 - Programming guidelines](https://goto.netcompany.com/cases/GTE711/FINSCHAT/Deliverables/DD160%20-%20Programming%20Guidelines/DD160%20-%20Programming%20Guidelines%20Schatkistbankieren.docx)


---
**Some common suggestions:**
1. The log content should be cleared
```csharp
// instead
logger.LogInformation("Changing execute status started");
// do this
logger.LogInformation("Changing execute status for activity id {ActivityId} to {ExecuteStatus} started", request.ActivityId, request.ExecuteStatus);
```


---

2. The entry/exiting logs should sync to each other (according to guideline)
```csharp
logger.LogInformation("Creating accounting period {PeriodNumber} started", request.Period
;
....
await unitOfWork.SaveChangesAsync(cancellationToken);
logger.LogDebug("New AccoutingPeriod with period number {PeriodNumber} was created with id {AccoutingPeriodId}", addedEntity.PeriodNumber, addedEntity.Id);
....
logger.LogInformation("Creating accounting period {PeriodNumber} succedded. Result: {@Result}", request.PeriodNumber, result);
```


---
3. The exiting log should contain the method's return value if possible and not sensitive
```csharp
logger.LogInformation("Creating accounting period {PeriodNumber} succedded. Result: {@Result}", request.PeriodNumber, result);
```


---
4. LogWarning should happen before Exception
```csharp
logger.LogWarning("Account with number {AccountNumber} was already existed", accountNumber);
throw new ValidationException(.....);
```


---
5. Almost if/else, switch/case or other kind of branching should be covered with LogDebug/Trace
```csharp
if (existedAccount != null)
{
    logger.LogDebug("Account with number {AccountNumber} was already existed, updating", existedAccount.AccountNumber);
}
else
{
    logger.LogDebug("Creating new account with number {AccountNumber}", request.AccountNumber);
    ....
    logger.LogDebug("New TreasuryAccount with number {AccountNumber} was created with entity id {TreasuryAccountEntityId}", createdTreasuryAccount.EntityId);
}

//
MappedMutationType result;
switch (mutationType)
{
    case MutationType.B:
    {
        result = MappedMutationType.B;
        break;
    }
    case MutationType.MEM:
    {
        result = MappedMutationType.MEM;
        break;
    }
    default:
    {
         logger.LogWarning("Unsupported mutation type {UnsupportedMutationType}", mutationType);
         throw new NotSupportedException(...);
    }
}

logger.LogTrace("Mutation type {MutationType}", mutationType);
logger.LogTrace("Mapped mutation type {MutationType}", result);
```


---
6. LogTrace some value used for calculation or some variable can significantly change the action/result behavior, especially complex calculation handler
```csharp
var eodBalance = previousBalance + totalCredit - totalDebit;
logger.LogTrace("EOD calculation - previous balance: {PreviousBalance}", previousBalance);
logger.LogTrace("EOD calculation - total credit: {TotalCredit}", totalCredit);
logger.LogTrace("EOD calculation - total debit: {TotalDebit}", totalDebit);
logger.LogTrace("EOD calculation: {EodBalance}", eodBalance);
```


---
7. Log some information assisting in debugging condition expression, don't hesitate to change the code a bit, valuable log content is important
```csharp
var requestedCaseIds = ...;
var foundCaseIds = ...;

var notFoundCaseIds = requestedCaseIds.Except(foundCaseIds);
if (notFoundCaseIds.Any())
{
    log.LogDebug/Warning("Could not found these case id {@NotFoundCaseIds}", notFoundCaseIds);
    ....
}
```


---
8. LogDebug some changing in database, such as new entity was added (which what id), such as removed entity (and which ids were removed)