1

Is there a way of logging the request and response from the client layer(not from controller as we can use middleware to log the same there).

I am looking to eliminate developer code for audit log here (//log request ,//log response and and creating a provider context ) instead move them to a common handler , may be inherit from delegating handler delegating handler and have the Audit log code there.

Any ideas ?

Currently we have audit logging in the client where another service is called but the developer has to do the following :

Client layer code:

    {
        IRestResponse response = null;
        ConnectorHTMLResponse CCMSResponse = null;

        request.Validate(request.TemplateName);

        var providerContext = _messageTracker.CreateProviderContext(correlationId, "MailTrigger", "GetHTML", OperationProtocols.HTTPS);

      //log request  
      await providerContext.StartAsync(request, param => request.TemplateName);

        var bodyJson = ToBodyJson(request, TemplateType.HTML);

        try
        {
            response = await ExecuteAsync(bodyJson, correlationId);
        }
        catch (Exception ex)
        {
            await providerContext.RaiseExceptionAsync(ex);
            throw;
        }
        Response = ConstructHTMLDocumentDetails(ValidateResponse(response));

        //log response
            await providerContext.CompletedAsync(Response);

        return Response;
    }

//and in the message tracker(Common code ) 

 public static ProviderContext CreateProviderContext(this IMessageTracker messageTracker, string correlationId, string systemId, string operationName, OperationProtocols protocol)
    {
        var context = new ProviderContext(
            messageTracker,
            correlationId,
            systemId,
            operationName,
            Assembly.GetCallingAssembly().GetName().Name,
            protocol
        );

        return context;
    }

 public async Task StartAsync<T>(T payload, Func<T, string> primaryIdentifierFunc = null, Func<T, string> secondaryIdentifierFunc = null)
    {
        await StartAsync(payload, primaryIdentifierFunc?.Invoke(payload), secondaryIdentifierFunc?.Invoke(payload));
    }

 public async Task CompletedAsync<T>(T payload, Func<T, string> primaryIdentifierFunc = null, Func<T, string> secondaryIdentifierFunc = null)
    {
        _source.Payload = payload.AsPayload();
        _source.PrimaryIdentifier = primaryIdentifierFunc?.Invoke(payload) ?? _source.PrimaryIdentifier;
        _source.SecondaryIdentifier = secondaryIdentifierFunc?.Invoke(payload) ?? _source.SecondaryIdentifier;

        await _tracker.TrackProviderResponseAsync(
           //track in cloud
        );
    }``

0 Answers0