我正在使用Asp.net MVC,我正在尝试通过将发布数据写入日志文件来调试我的方法。
我接受了这篇文章C# .net equivilent of HTTP_RAW_POST_DATA?并尝试使用它。
这是我的ActionResult
:
public ActionResult Receipt(string id)
{
try
{
using (var sw = new StreamWriter(@"C:\LOGS\RECEIPTLOG.TXT", true))
{
sw.WriteLine("-------------------------------------------");
sw.WriteLine("Start Receipt: " + DateTime.Now);
sw.WriteLine("Browser: " + Request.Browser.Id);
sw.WriteLine("Browser Version: {0}.{1}", Request.Browser.MajorVersion, Request.Browser.MinorVersion);
sw.WriteLine("Request.UserHostAddress: " + Request.UserHostAddress);
if (Request.UrlReferrer != null)
{
sw.WriteLine("Request.UrlReferrer: " + Request.UrlReferrer);
}
foreach (var headerKey in Request.Headers.Keys)
{
var headerValues = Response.Headers.GetValues(headerKey.ToString());
if (headerValues != null)
{
sw.WriteLine("Response Header: {0}, Value: {1}", headerKey, String.Join(";", headerValues));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message);
}
var control = Logging.StartLog();
control.Source = SourceEnum.WebPortal;
try
{
Logging.WriteLog("Start Receipt");
var count = 0;
var postVals = new Dictionary<string, string>();
foreach (var key in Request.Form.AllKeys)
{
Logging.WriteLog("count: " + count);
Logging.WriteLog(string.Format("key: {0} Value: {1}", key, Request.Form[key]));
postVals.Add(key, Request.Form[key]);
Logging.WriteLog("finished count: " + count);
count++;
}
Logging.WriteLog("finished processing ALLKeys");
var paymentReq = createPaymentRequest(postVals);
Logging.WriteLog("finished processing 'var paymentReq = createPaymentRequest(postVals)' ");
Logging.WriteLog("_client.RecordPaymentWithRequest start");
var receipt = _client.RecordPaymentWithRequest(paymentReq);
Logging.WriteLog("Running _client.RecordPaymentWithRequest complete");
var retval = PartialView(receipt.Duplicate ? "Duplicate Receipt" : "Receipt", receipt);
Logging.WriteLog(string.Format("retval: {0}", retval));
return retval;
}
catch (Exception ex)
{
Logging.WriteException(ex);
throw;
}
finally
{
Logging.WriteLog(control, "End RegisterNewAccountEmail");
try
{
using (var sw = new StreamWriter(@"C:\LOGS\RECEIPTLOG.TXT", true))
{
sw.WriteLine("Stop Receipt: " + DateTime.Now);
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message);
}
}
}
这就是我的日志中的内容:
-------------------------------------------
Start Receipt: 1/14/2014 7:17:07 AM
Browser: internetexplorer
Browser Version: 11.0
Request.UserHostAddress: xxx.xxx.xxx.xxx
Request.UrlReferrer: website url here
Stop Receipt: 1/14/2014 7:17:07 AM
正如您所看到的,没有写入标题信息。
我是否将此错误转移到日志文件中?如果没有,为什么我的标题信息没有写入日志?
**更新**
我在第一次尝试/捕获时将其添加到我的日志中:
sw.WriteLine("Request.Headers.Keys.Count: " + Request.Headers.Keys.Count);
这就是结果:
Request.Headers.Keys.Count: 11
答案 0 :(得分:1)
使用Request.Headers.AllKeys而不是Keys。
AllKeys返回一个你想要的字符串数组 Keys会返回NameObjectCollectionBase.KeysCollection Class