0

我正在尝试使用 Application Insights 进行一些日志记录,并且我想记录每个依赖项调用。

我根据以下关于如何设置标头值的 SO 回答来执行此操作:link

在 RequestResponseAsync 方法中,我创建了我的 DependencyTelemetry 对象,并且能够将服务记录到调用的目的地、持续时间等。但是,我还希望能够设置在服务内部调用的方法名称,但是我似乎无法在任何地方找到它。我们正在谈论 WebApi 到 SF 服务调用和 SF 服务到 SF 服务调用。

有人可以告诉我我是否在正确的路径上,或者我是否应该覆盖其他方法。

这是我基于之前链接的 SO 答案的自定义代码:

public async Task<IServiceRemotingResponseMessage> 
RequestResponseAsync(IServiceRemotingRequestMessage requestMessage)
{
    var correlationId = SetCorrelationId(requestMessage);
    IServiceRemotingResponseMessage response = null;

    var startTime = DateTime.UtcNow;
    var timer = Stopwatch.StartNew();

    try
    {
        response = await _inner.RequestResponseAsync(requestMessage);
        timer.Stop();
        return response;
    }
    finally
    {
        CreateDependencyTelemetry(correlationId, _inner.ResolvedServicePartition.ServiceName.AbsoluteUri, startTime, timer.Elapsed, response != null, _inner.ResolvedServicePartition.ServiceName.AbsoluteUri, response == null ? "400" : "200");
    }
}

private static string SetCorrelationId(IServiceRemotingRequestMessage requestMessage)
{
    string correlationId;

    if (Activity.Current != null)
        correlationId = Activity.Current.RootId;
    else
        correlationId = (string)CallContext.GetData(Constants.CorrelationId);

    requestMessage.GetHeader().AddHeader(Constants.CorrelationId, Encoding.ASCII.GetBytes(correlationId));

    return correlationId;
}

private void CreateDependencyTelemetry(string correlationId,string target, DateTime startTime, TimeSpan duration, bool success, string name, string resultCode = "200")
{
    var dependencyTelemetry = new DependencyTelemetry();
    dependencyTelemetry.Context.Operation.Id = correlationId;
    dependencyTelemetry.Type = "ServiceFabricServiceRemoting";
    dependencyTelemetry.ResultCode = resultCode; //TODO: Create better result codes?
    dependencyTelemetry.Name = name; //TODO: Provide specific method name
    dependencyTelemetry.Success = success;
    dependencyTelemetry.Duration = duration;
    dependencyTelemetry.Timestamp = startTime;
    dependencyTelemetry.Target = target;

    _telemetryClient.TrackDependency(dependencyTelemetry);
}

这会生成依赖项跟踪日志,其中依赖项的名称如下所示:fabric:/ProjectName/ServiceName

我想让它看起来像这样:fabric:/ProjectName/Servicename/ ServiceMethodName

如果这会有所作为,我有无状态服务。

任何帮助深表感谢

4

0 回答 0