Implementing the logging part

In this section, we will learn how to perform logging in the catalog web service. Let's start by choosing a layer where we'll execute the logging statements. Since the logic is encapsulated in the Catalog.Domain layer project, we will continue by implementing the logging part on the service classes defined in the project. First of all, let's start by defining a new logging class, which contains the corresponding event id for each operation:

namespace Catalog.Domain.Logging
{
public class Events
{
public const int Get = 1000;
public const int GetById = 1001;
public const int Add = 1002;
public const int Edit = 1003;
public const int Delete = 1004;
}
}

Once we have established a corresponding log event id for each activity, we need to define the logging messages that will be used by the ILogger interface. For now, we can determine the following messages:

namespace Catalog.Domain.Logging
{
public class Messages
{
public const string NumberOfRecordAffected_modifiedRecords =
"Number of record affected {records}";
public const string ChangesApplied_id = "Changes applied to the
following entity id ({id})"
;
public const string TargetEntityChanged_id = "Target entity id
({id})"
;
}
}

The first one refers to the number of records affected by a change, while the second is the entity that's been changed. Finally, the third one provides a message for the target entity of the handler. It is essential to note that both constants follow a naming convention: the first part of their name refers to the content of the message; after the first underscore, we have the parameters that will be replaced by values when we use the logging templating system.

Furthermore, we can proceed by changing the IItemService methods by implementing logging. Let's start with the AddItemAsync method:

using Microsoft.Extensions.Logging;
using Catalog.Domain.Logging;
...

namespace Catalog.Domain.Services
{
public class ItemService : IItemService
{
private readonly IItemMapper _itemMapper;
private readonly IItemRepository _itemRepository;
private readonly ILogger<IItemService> _logger;

public ItemService(IItemRepository itemRepository,
IItemMapper itemMapper, ILogger<IItemService> logger)
{
_itemRepository = itemRepository;
_itemMapper = itemMapper;
_logger = logger;
}

...

public async Task<ItemResponse> AddItemAsync(AddItemRequest
request)
{
var item = _itemMapper.Map(request);
var result = _itemRepository.Add(item);

var modifiedRecords = await _itemRepository.
UnitOfWork.SaveChangesAsync();

_logger.LogInformation(Events.Add, Messages.
NumberOfRecordAffected_modifiedRecords, modifiedRecords);

_logger.LogInformation(Events.Add, Messages.
ChangesApplied_id, result?.Id);


return _itemMapper.Map(result);
}

...
}
}

The preceding code tracks the information about the number of affected records and the id of the added record. We can do the same with the other methods of the ItemService class. In the case of read-only operations, we can add the id of the target record; for example, in the case of the GetItemAsync method:

public class ItemService : IItemService
{
...

public async Task<ItemResponse> GetItemAsync(GetItemRequest
request)
{
if (request?.Id == null) throw new ArgumentNullException();
var entity = await _itemRepository.GetAsync(request.Id);

_logger.LogInformation(Events.GetById,
Messages.TargetEntityChanged_id, entity?.Id);


return _itemMapper.Map(entity);
}

The preceding code logs the information related to the id that was retrieved by the service by using the Event.GetById field. It uses the Messages type to specify the event message. In the next section, we will learn how to log exceptions by enhancing the exception handling implementation of logs.  

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.175.180