Logging 如何每天将asp.net webapi中的请求和响应记录为文本文件?
我需要使用asp.net webAPI将请求和响应记录在文本文件中。提供一个好的简单例子。我听说过使用委托处理程序,我们可以实现如下Logging 如何每天将asp.net webapi中的请求和响应记录为文本文件?,logging,asp.net-web-api2,Logging,Asp.net Web Api2,我需要使用asp.net webAPI将请求和响应记录在文本文件中。提供一个好的简单例子。我听说过使用委托处理程序,我们可以实现如下 public class LogRequestAndResponseHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, Cancellatio
public class LogRequestAndResponseHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
//logging request body
string requestBody = await request.Content.ReadAsStringAsync();
Trace.WriteLine(requestBody);
//let other handlers process the request
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
//once response is ready, log it
var responseBody = task.Result.Content;
Trace.WriteLine (responseBody);
return task.Result;
});
}
}
公共类LogRequestAndResponseHandler:DelegatingHandler
{
受保护的覆盖异步任务SendAsync(
HttpRequestMessage请求,CancellationToken CancellationToken)
{
//日志记录请求主体
string requestBody=wait request.Content.ReadAsStringAsync();
Trace.WriteLine(请求主体);
//让其他处理程序处理该请求
return wait base.sendaync(请求、取消令牌)
.ContinueWith(任务=>
{
//一旦响应就绪,记录它
var responseBody=task.Result.Content;
Trace.WriteLine(responseBody);
返回任务。结果;
});
}
}
但是如何每天将请求和响应保存在文本文件中。提供一个很好的简单示例。不确定您是否想要这样的东西,尽管您可以根据需要进行调整:
public class LogRequestAndResponseHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
//logging request body
string requestBody = await request.Content.ReadAsStringAsync();
Trace.WriteLine(requestBody);
//let other handlers process the request
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var responseBody = task.Result.Content;
Trace.WriteLine (responseBody);
// Write Text File. If file exists, appends. If file doesn't exist, creates.
string today = DateTime.Now.toString();
System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(Server.MapPath(today + ".txt"), true);
StreamWriter1.WriteLine(responseBody);
StreamWriter1.Close();
return task.Result;
});
}
}
公共类LogRequestAndResponseHandler:DelegatingHandler
{
受保护的覆盖异步任务SendAsync(
HttpRequestMessage请求,CancellationToken CancellationToken)
{
//日志记录请求主体
string requestBody=wait request.Content.ReadAsStringAsync();
Trace.WriteLine(请求主体);
//让其他处理程序处理该请求
return wait base.sendaync(请求、取消令牌)
.ContinueWith(任务=>
{
var responseBody=task.Result.Content;
Trace.WriteLine(responseBody);
//写入文本文件。如果文件存在,则追加。如果文件不存在,则创建。
string today=DateTime.Now.toString();
System.IO.StreamWriter StreamWriter1=new System.IO.StreamWriter(Server.MapPath(today+“.txt”),true;
StreamWriter1.WriteLine(responseBody);
StreamWriter1.Close();
返回任务。结果;
});
}
}
除了尤塞尔:
您需要检查Result.Content是否有值,因为对API的选项调用不会返回正文,代码将失败
修正:
if (task.Result.Content != null)
{
var responseBody = task.Result.Content.ReadAsStringAsync().Result;
...
}