jeudi 21 février 2019

Loose coupling for logging classes

I'm trying to make a class (actually 2) that will encapsulate some specific logging/telemetry technology. They are used in a decorator to log requests, like so:

var manager = new SpecificTelemetryManager();
....
public void PostRequest(string body) 
{
    var telem = manager.Start();
    controller.PostRequest();
    manager.Stop(telem);
}

I have this:

ITelemetryManager.cs

public interface ITelemetryManager
{
    ITelemetryOperation Start(string operationName);

    void Stop(ITelemetryOperation op);

}

ITelemetryOperation.cs

public interface ITelemetryOperation<T> : IDisposable
{

    T Operation { get; set; }

    void AddCustomProperties(IDictionary<string, string> customProperties);

}

SpecificTelemetryManager.cs (does not compile)

public class SpecificTelemetryHandler : ITelemetryManager
{

    private Client _client { get; set; }

    public SpecificTelemetryHandler()
    {
        _client = new Client();
    }

    public ITelemetryOperation Start(string name)
    {
        return new SpecificTelemetryOperation(_client.Start(name));
    }

    public void Stop(ITelemetryOperation op)
    {
        _client.StopOperation(op.Operation); // !!! THIS DOESNT KNOW THE TYPE
    }

}

SpecificTelemetryOperation.cs

public class SpecificTelemetryOperation : ITelemetryOperation<SomeMicrosoftClass>
{
    public SomeMicrosoftClass Operation { get; set; }

    public ApplicationInsightsTelemetryOperation(SomeMicrosoftClass op)
    {
        Operation = op;
    }

    public void AddCustomProperties(IDictionary<string, string> customProperties)
    {
        Operation.Props.AddRange(customProperties);
    }

    public void Dispose()
    {
        Operation.Dispose();
    }

}

Can anyone please provide some ideas on how to keep ITelemetryManager typeless (non generic) (so it won't be ITelemetryManager<Iwhatever>) and make sure that outside classes (the decorator) don't know what type of telemetry operations they are working on (Microsoft, AWS, Google, etc...). I want SpecificTelemetryManager and SpecificTelemetryOperation to have all the concrete classes that they would be referencing.

Aucun commentaire:

Enregistrer un commentaire