我在为ServiceStack.JsonServiceClient
启用日志记录时遇到问题。我正在使用文档"捕获.NET服务客户端中的HTTP标头",但我一定遗漏了一些内容,因为我只得到了一个空的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
,你的调用只设置了前两个标志,但你的代码依赖于捕获日志来打印出来。在这种情况下,你想指定它不应该在每次请求后用清除HttpLog
Api.CaptureHttp(clear:false)