让NLog发送带有正确标头的JSON?

acp*_*acp 8 c# logging json couchdb nlog

尝试使用NLog从我们的C#app发送JSON编码的日志,将它们转储到CouchDB中.但是,使用网络目标,我似乎无法找到正确设置Content-Type标头的方法; 否则CouchDB与我的输入无关.

CouchDB是开箱即用的当前NLog配置:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
    <variable name="FileLayout" value='{ "date":"${longdate}","level":"${level}","message":${message}}' />
    <targets>
      <target name="logfile" xsi:type="Network" address="http://127.0.0.1:5984/logger/" layout="${FileLayout}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
    </rules>
  </nlog>
Run Code Online (Sandbox Code Playgroud)

并且原始HTTP(主机名已编辑):

POST http://127.0.0.1:5984/logger/ HTTP/1.1
Host: 127.0.0.1:5984
Content-Length: 221
Expect: 100-continue

{ "date":"2011-08-12 13:38:06.4603","level":"Info","message":{"backlog":0, "too_busy":0, "io_threads":1000, "threads":32766, "messages_processed":0}, "ProcessName": "foobar"}
Run Code Online (Sandbox Code Playgroud)

Bro*_*ook 4

查看HttpNetworkSender 源代码,我没有看到将内容类型传递给WebRequest.

我认为您必须基于NetworkTarget使用自定义创建一个自定义目标HttpNetworkSender,并包含配置以适当地设置内容类型WebRequest