Datadog Log Integration In Asp.Net Core Web API

In this blog, we are talking about Datadog log integration in asp.net core web API. Datadog helps you to filter your logs using tags. Sometimes, your infrastructure may generate a volume of log events that is too large or has significant fluctuations. In this situation, you may need to choose which logs to send to a log management solution, and which logs to archive. However, filtering your logs before sending them may lead to gaps in coverage or the accidental removal of valuable data.

Firstly, You can create an account in Datadog and get the API key. If you haven’t, you can see my previous blog about creating a DataDog account and getting the API key. here

I hope, you have the Datadog API key. Now we can start the Datadog log collection in asp.net.

To send your C# logs to Datadog, we recommend logging into a file and then tailing that file with your Datadog site. Here are setup examples for the  Serilog, NLog, and log4net logging libraries.

In this article, we are going with Agentless logging. It is possible to stream logs from your application to Datadog or the Datadog Agent directly. This is not the recommended setup as handling connection issues should not be done directly in your application, but it might not be possible to log to a file when your application is running on a machine that cannot be accessed.

In Agentless logging we are using Serilog. 

Install the Datadog Serilog sink, which sends events and logs to Datadog. By default, the sink forwards logs through HTTPS on port 443. Run the following command in the Package Manager Console:

PM> Install-Package Serilog.Sinks.Datadog.Logs

Then, initialize the logger directly in your application. Do not forget to add your <API_KEY>.

using (var log = new LoggerConfiguration()
    .WriteTo.DatadogLogs("<API_KEY>", configuration: new DatadogConfiguration { Url = "https://http-intake.logs.datadoghq.com" })
    .CreateLogger())
{
    // Some code
}

You can also override the default behavior and forward logs in TCP by manually specifying the following required properties: URL, port, use SSL, and TCP. Optionally, specify the source, service, host, and custom tags. we will talk about the custom tags in our next blog.

For instance, to forward logs to the Datadog US region in TCP you would use the following sink configuration:

var config = new DatadogConfiguration(url: "intake.logs.datadoghq.com", port: 10516, useSSL: true, useTCP: true);
using (var log = new LoggerConfiguration()
    .WriteTo.DatadogLogs(
        "<API_KEY>",
        configuration: config
    )
    .CreateLogger())
{
   Exception ex = new Exception();
   var message = "this is log message";
   
   //Error Log
   log.Error("Error Message {@ErrorMessage}, Exception {@Exception} ", message, ex);

  //Info Log
   log.Information("Info Message {@Message}", message);

  //Warning Log
  log.Warning("Warning Message {@Message}", message);
}

New logs are now directly sent to Datadog.

I hope you guys understand how I can do this.  Let me know if you face any difficulties.

You can watch my previous blog here. and next blog.

Happy Coding {;} ????

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories