使用 C# 和 .NET 的 Datadog 日志

Datadog logs with C# and .NET

我正在尝试从我的 C# 应用程序 post Datadog-logs。我设法使用 Postman 发送具有所需结构的日志,但我只是无法弄清楚如何从代码中实现相同的目标。 我尝试了什么:

  1. 使用 DogStatsD - 但我不想安装代理,我更愿意使用 Datadog REST API 来 post 我的日志。
  2. 使用 Serilog.Sinks.Datadog.Logs - 这似乎很容易使用,但我不知道它是如何工作的,以及是否可以更改日志结构。默认情况下,结果 json 中有 MessageTemplate 和 Properties 字段。我希望能够在一条消息中发送我自己的结构,而不是使用 MessageTemplate。这可能吗?

The desired log to be seen in Datadog UI Logs Section:

{
    hostname: myHost
    myStuff {   
    item1: item_val1
    item2: item_val2
    }
    otherStuff: oh wow this is cool
    service: MyService
}

Here's what I sent using Postman to achieve this result:

URL: https://http-intake.logs.datadoghq.com/v1/input

Headers:

DD-API-KEY: my_api_key
Content-Type: application/json

Body:

{
    "ddsource": "mySource",
    "ddtags": "myTag: myVal, myValWithoutTag",
    "hostname": "myHost",
    "message": {
        "myStuff":
        {
            "item1": "item_val1",
            "item2": "item_val2"
        },
        "otherStuff": "oh wow this is cool"
    },
    "service": "MyService"
}

是否可以使用 datalog serilog sinks 获得相同(甚至相似)的结果?如果没有,我如何在 C# 中实现此结果?

这是我从代码中尝试的:

var config = new DatadogConfiguration(url: "intake.logs.datadoghq.com", port: 443, useSSL: true, useTCP: true);
using (var log = new LoggerConfiguration().WriteTo.DatadogLogs(
        "<myApiKey>",
        source: "mySource",
        service: "myService",
        host: "myHost",
        tags: new string[] {"myTag:myVal", "myValWithoutTag"},
        configuration: config
    ).
    CreateLogger())
{
    var messageTemplate = "{message}";
    var message = new
    {
        myStuff = new
        {
            item1 = "item_val1",
            item2 = "item_val2"
        }
    };

    log.Information(messageTemplate, message);
}

Datadog UI 日志部分出现意外结果:

{
    host: myHost
    level: Information
    MessageTemplate: {message}
    Properties: {   
    message: { myStuff = { item1 = item_val1, item2 = item_val2 } }
    }
    service: myService
    Timestamp: 2021-05-17T00:13:14.2614896+03:00
}

标签部分有效,主机和服务部分也一样。我不介意添加级别和时间戳, 但我很想将 body 更改为像 Postman 示例中那样的行为(只是 JSON 中的消息)。 所以我的问题是:

谢谢!

您真的不需要使用 Agent 来向 Datadog 发送和自定义日志。提供跟踪和其他指标非常酷。不幸的是,无法在 Datadog 日志记录配置中更改 MessageTemplate。只在 Serilog 中放入 MessageTemplate 之类的消息:

var message = "Service started";
logger.Information(message);

您可以通过 2 种方式添加属性。 使用 Serilog Enrich:

var config = new DatadogConfiguration(url: "intake.logs.datadoghq.com", port: 443, useSSL: true, useTCP: true);
using (var log = new LoggerConfiguration()
    .Enrich.WithProperty(item1, item_val1)
    .WriteTo.DatadogLogs(
      "<myApiKey>",
      source: "mySource",
      service: "myService",
      host: "myHost",
      tags: new string[] {"myTag:myVal", "myValWithoutTag"},
      configuration: config
).
CreateLogger())

或者通过中间件在 LogContext 中推送 属性:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Serilog.Context;

namespace MyProject.Middlewares
{
  public class LogPropertyMiddleware
  {
    private readonly RequestDelegate _next;

    public LogUserInfoMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        LogContext.PushProperty("item1", "item_val1");

        await _next.Invoke(context);
    }
  }
}

将中间件添加到启动:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   .....
   .....
   app.UseMiddleware<LogPropertyMiddleware>();
   .....
}