简体   繁体   中英

ServiceStack.JsonServiceClient.HttpLog is not populating

I'm having trouble enabling logging for a ServiceStack.JsonServiceClient . I'm working from the documentation Capture HTTP Headers in .NET Service Clients but I must be missing something becasue I'm only ever getting an empty string .

Here's my code:

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()
}
    

Because I have Api.CaptureHttp(true, true) it is logging the info to the test log but I want to be able to capture the httpLog in code and like I said, it's only ever an empty string there.

CaptureHttp has 3 flags:

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

You're only setting Api.CaptureHttp(print:true,log:true) which should print to the console and log to your logger if IsDebugEnabled , your call only sets the first 2 flags but you're code is relying on capturing the log to print it out in which case you wanted to specify that it shouldn't clear the HttpLog after each request with:

Api.CaptureHttp(clear:false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM