获取NLOG发出JSON适当的头? [英] Getting NLog to send out JSON with proper headers?

查看:507
本文介绍了获取NLOG发出JSON适当的头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用NLOG发送JSON编码的注销我们的C#应用​​程序,将它们转储到CouchDB的。但是,使用网络的目标,我似乎无法找到一个方法来正确设置Content-Type头;和CouchDB的不会有任何与我的输入,否则。



CouchDB是外的开箱,目前NLOG配置:

 < n日志的xmlns =http://www.nlog-project.org/schemas/NLog.xsd
的xmlns:XSI =http://www.w3.org/2001/XMLSchema-例如throwExceptions =真>
<变量名=FileLayout值='{日期:$ {的longdate},级别:$ {水平},消息:$ {消息}}'/> ;
<靶标GT;
<目标名称=日志文的xsi:type =网络的地址=http://127.0.0.1:5984/logger/布局=$ {} FileLayout/>
< /目标>
<规则与GT;
<记录器名称=*定义的MinLevel =跟踪中的writeTo =日志文/>
< /规则>
< / n日志>

和原始的HTTP(主机名编辑):

  POST http://127.0.0.1:5984/logger/ HTTP / 1.1 
主机:127.0.0.1:5984
的Content-Length:221
期望:100-继续

{日期:2011-08-12 13:38:06.4603,水平:信息,消息:{积压: 0,too_busy:0,io_threads:1000年,线程:32766,messages_processed:0},ProcessName:foobar的}


解决方案

综观 HttpNetworkSender 来源,我没有看到一个明显的方式在内容类型传递在的WebRequest



我认为你将不得不创建一个基于 NetworkTarget 它使用一个自定义的 HttpNetworkSender ,并包括配置来设置内容类型上的的WebRequest 恰如其分。


Trying to use NLog to send JSON-encoded logs out of our C# app, to dump them into CouchDB. However, using the Network target, I can't seem to find a way to set the Content-Type header properly; and CouchDB won't have anything to do with my input otherwise.

CouchDB is out-of-the-box, Current NLog config:

<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>

And the raw HTTP (hostnames edited):

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"}

解决方案

Looking at the HttpNetworkSender source, I don't see an obvious way to pass in a content type to the WebRequest.

I think you will have to create a custom target based on the NetworkTarget which uses a custom HttpNetworkSender, and include config to set the content-type on the WebRequest Appropriately.

这篇关于获取NLOG发出JSON适当的头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆