代码之家  ›  专栏  ›  技术社区  ›  LorneCash

ServiceStack。JsonServiceClient。HttpLog未填充

  •  0
  • LorneCash  · 技术社区  · 3 年前

    我无法为 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,正如我所说的,它只是一个空的 一串 那里

    0 回复  |  直到 3 年前
        1
  •  1
  •   mythz    3 年前

    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)