ServiceStack.JsonServiceClient.HttpLog 未填充

ServiceStack.JsonServiceClient.HttpLog is not populating

我无法为 ServiceStack.JsonServiceClient 启用日志记录。我正在根据文档 Capture HTTP Headers in .NET Service Clients 工作,但我一定遗漏了一些东西,因为我只得到一个空的 string.

这是我的代码:

public class Client
{
    private bool Logging { get; }
    
    public Client(bool logging = false)
    {
        Api = new("https://api.service.com/");
        Logging = logging;
        if (Logging) Api.CaptureHttp(true, true);
    }

    public string GetInfo(string name)
    {
        if (Logging) Api.HttpLog.Clear();
        var response = Api.Get(new Requests.GetInfo(name));
        Debug.WriteLine(Api.HttpLog.ToString());
        return response.Result;
    }
}

[Route("/v1/getinfo/{name}")]
[DataContract]
public class GetInfo : Base, IReturn<Responses.GetInfo>
{
    public GetInfo(string name) => Name = name;

    [DataMember(Name = "name")]
    public string Name { get; init; }
}


[TestMethod]
public void Client_GetInfo()
{
    var client = new Client(true);
    client.GetInfo()
}
    

因为我 Api.CaptureHttp(true, true) 它正在将信息记录到测试日志中,但我希望能够在代码中捕获 httpLog,就像我说的那样,它只是一个空的 string .

CaptureHttp 有 3 个标志:

public void CaptureHttp(bool print = false, bool log = false, bool clear = true)

你只设置了 Api.CaptureHttp(print:true,log:true) 如果 IsDebugEnabled,它应该打印到控制台并记录到你的记录器,你的调用只设置了前 2 个标志,但你的代码依赖于捕获打印出来的日志,在这种情况下你想指定它不应该在每次请求后清除 HttpLog:

Api.CaptureHttp(clear:false)