用于Serilolg的Graylog接收器添加http上下文数据

时间:2019-03-12 18:15:47

标签: .net serilog graylog2 graylog graylog3

我正在尝试使用graylog接收器将日志记录添加到.net核心Web api。 所需的输出将与使用formmater的控制台上的输出相同,如下所示:

  

“ [[{Timestamp:HH:mm:ss} {Level:u3}] {EventId} {Message:lj} {Properties} {NewLine} {Exception} {NewLine}”)

产生该输出:

[18:10:03 INF] { Id: 2 } Request finished in 876.4304ms 200 
text/plain; charset=utf-8 {ElapsedMilliseconds=876.4304, 
StatusCode=200, ContentType="text/plain; charset=utf-8", 
SourceContext="Microsoft.AspNetCore.Hosting.Internal.WebHost", 
RequestId="0HLL72NNJ19OK:00000001", RequestPath="/geo/allmenu", 
CorrelationId=null, ConnectionId="0HLL72NNJ19OK"}

我对Graylog的接收器配置:

  var graylogConfig = new GraylogSinkConfiguration
        {

            Host = "10.1.1.100",
            Port = 12201,
            UseSecureConnection = false,
            UseAsyncLogging = true,
            GraylogTransportType = GraylogTransportType.Tcp,
            RetryCount = 20,
            PropertyPrefix = "webapi",                

        };

不幸的是,我无法在Graylog接收器中看到任何丰富的数据,并且想知道如何为该接收器提供格式化程序?

1 个答案:

答案 0 :(得分:0)

为解决此问题,我决定在asp.net核心应用程序中使用syslog接收器,这使我可以附加所有丰富的日志数据。

        // Use custom message template formatters, just so we can distinguish which sink wrote each message
        var tcpTemplateFormatter = new MessageTemplateTextFormatter("[{Timestamp:HH:mm:ss} {Level:u3}] {EventId} {Message:lj} {Properties}{NewLine}{Exception}{NewLine}{Message}", null);


        // Log RFC5424 formatted events to a syslog server 
        var tcpConfig = new SyslogTcpConfig
        {
            Host = "10.1.1.100",
            Port = 12201,
            Formatter = new Rfc5424Formatter(templateFormatter: tcpTemplateFormatter),
            Framer = new MessageFramer(FramingType.OCTET_COUNTING),
            KeepAlive = true
        };