How To Create And Use Custom Tags In DataDog Log Using Asp.net Core Web API.

In this blog, we will talk about creating custom tags in the Datadog log, and also we will discuss how to send hostname, service name, and log template in the Datadog.

you can watch my previous blog on how to send logs in asp.net core web API. here

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

  • Creating a DataDog account and getting the API key. Link
  • Datadog Log Integration In Asp.Net Core Web API. Link

Let’s start,

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  SerilogNLog, and log4net logging libraries.

In this article, we are going with Agentless logging. 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>",
         host: "<HOST_NAME>",
         source: "<SOURCE_NAME>",
         service: "<SERVICE_NAME>",
         tags: new string[] {"<TAG_1>:<VALUE_1>", "<TAG_2>:<VALUE_2>"}
    )
    .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.

You can see in the above image hostname, service name, source name, and error message. In this log management dashboard, you can search with your tag name and filter the log. you can create your own error template as per your requirement.

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 *

Footer Logo

Subscribe

Select Categories