Categories: .NET Core

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 {;} ????

Nayan Raval

Nayan Raval is a MEAN Stack .Net Developer has extensive experience with designing and developing enterprise-scale applications. Key Areas Of Expertise: • ASP.NET Core MVC • ASP.NET Core Web API • C# • ASP.NET MVC 5 • Angular All versions • HTML5 • CSS3 / SCSS • Bootstrap • JavaScript • Azure • JQuery Databases and related • Microsoft SQL server MSSQL • PostgreSQL • Entity Framework (EF) • LINQ UI Frameworks • Kendo UI • Telerik • JQuery UI • Prime NG and Material UI API Integration • SignalR • DateDog • Twilio Voice Call And Message • Stripe • SendGrid (Email Camping) • Checkr • Zoom Video Call • Auth0 • Elastic Search • Quartz - Scheduler • JWT Token • Google Calendar

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago