【问题标题】:use nlog to write to rest service使用 nlog 写入 rest 服务
【发布时间】:2015-08-17 15:43:58
【问题描述】:

已经有一段时间了, 我有一个休息服务,它接受我通过查询字符串发送的一些参数。除此之外,我需要通过请求正文发送一些字段。 格式需要为 json。 这是一个来自邮递员的成功请求,例如:

{
  "TransactionLogId": "6e6279a3-22d9-458d-b1c9-9b03a81556be",
  "CreatedDate": "2015-08-17T15:05:50.0143866Z",
  "LogType": "Info",
  "TransactionCode": "2831b7bc-9fc8-424d-857a-182397a5eb11",
  "ServiceName": "ESBService.WCF",
  "ServiceId": "8664e362-f63d-4d10-8a23-3b86b9f22cc7",
  "Servers": "******************",
  "Context": "EmployersSite",
  "RequestIPs": "**************",
  "UserId": "258a8c83-3f18-40d6-aea7-986dc0d97656",
  "ActivityTime": "2015-08-17T15:05:50.0143866Z",
  "LogSubType": "Info.RequestBegin",
  "Title": "RequestBegin",
  "Details": "",
  "RequestData": "{\r\n  \"Filters\": {\r\n    \"Mode\": \"Automatic\",\r\n    \"Type\": \"Applicant\"\r\n  }\r\n}",
  "EntityClass": "SavedSearch",
  "EntityId": "",
  "Methods": "SavedSearch_Search; SetSession",
  "SourceFilePath": "*******************\\PortalService.svc.cs",
  "SourceLineNum": 1025,
  "RunTime": 0.015
}

这是我的配置。假设我现在想要的只是在详细信息字段中发送异常消息。

这是我的配置:

  <nlog throwExceptions="true" internalLogFile="c:\\Data\\Logs\\IES\\nlog_debug.txt" internalLogLevel="Warn">
    <variable name="LogBaseFolder" value="c:\\Data\\Logs\\tester" />
    <targets>
      <target type='WebService'
               name='ws'
               url='somesvc.vc/TransactionLog/Create?Context=Portal&amp;UserToken=a441b37f-3403-43fd-8f58-d1da3024133a'
               protocol='HttpPost'
               encoding='UTF-8'>
        <parameter name='details' type='System.String'  layout="  ${message}"/>
      </target>
    </targets>
 <rules>
      <logger name="*" writeTo="ws" />
    </rules>
  </nlog>

和我的代码:

Log.Site.Error("erorrrr")

但是请求永远不会让它得到正文不正确的错误:

message=解析值时遇到意外字符:M. 路径 '',第 0 行,第 0 位。

这真是要了我的命,真的需要一些帮助。谢谢

【问题讨论】:

    标签: json rest nlog


    【解决方案1】:

    不用担心,这实际上是新手经常遇到的问题,并且有一个简单的解决方案! – 所以请放心:) 所以基本上,当你在响应中传回 JSON 时,你需要在 response.Write() 之前对对象进行超级序列化,执行 response.super(对象名_对象) 它的作用是处理“更深”的封装过程(“DIYA 封装”),其中包括嵌套对象(方法和成员) 但没有核心函数(构造函数、析构函数..)

    希望对您有所帮助,祝您好运。

    顺便说一句,建议您阅读有关类、嵌套对象、什么是构造函数等的更多信息。 随着您作为开发人员的发展,它将为您提供更多帮助!干杯:)

    【讨论】:

    • 抱歉,您所说的与我的要求无关。 (顺便说一句,我从来没有群过(reponse.super ...),无论如何,这是我发布的json是邮递员(chrome.google.com/webstore/detail/postman/…)发出的一个好请求,我想做的就是发送相同类型的请求vua nlog 到休息服务。我可以在文件等中创建我想要的任何东西。问题是当它作为 json 发送到休息服务时。干杯:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多