Logging to DataDog with Serliog and .Net 5
Full source code available here.
This is a quick post to showing how to setup a .Net 5 Web Api application with logging to DataDog from your local computer.
Getting Datadog up and running
First step is to create an account with Datadog and then install the Datadog agent on your computer, in my case I am using Ubuntu.
There are a few small configuration steps to get the agent up and running.
In the datadog.yaml
file look for an entry called logs_enabled, make sure this is set to true.
1##################################
2## Log collection Configuration ##
3##################################
4
5## @param logs_enabled - boolean - optional - default: false
6## Enable Datadog Agent log collection by setting logs_enabled to true.
7#
8logs_enabled: true
In the /etc/datadog-agent/conf.d/
create a directory called mydotnet5app.d
.
Inside that directory create a file called conf.yaml
and paste in the below.
1#Log section
2logs:
3
4 # - type : (mandatory) type of log input source (tcp / udp / file)
5 # port / path : (mandatory) Set port if type is tcp or udp. Set path if type is file
6 # service : (mandatory) name of the service owning the log
7 # source : (mandatory) attribute that defines which integration is sending the log
8 # sourcecategory : (optional) Multiple value attribute. Can be used to refine the source attribute
9 # tags: (optional) add tags to each log collected
10
11 - type: file
12 path: /path/to/my/logfile/application.log
13 service: csharp
14 source: csharp
The .Net 5 Application
I have a simple .Net 5 Web Api application. I added Serilog to log to a file at the location specified in the conf.yaml
above.
In Program.cs
I add the below. Note the JsonFormatter
on line 4, that is important for structured logging in DataDog -
1public static void Main(string[] args)
2{
3 Log.Logger = new LoggerConfiguration()
4 .WriteTo.File(new JsonFormatter(renderMessage: true), "webapi.log")
5 .CreateLogger();
6
7 try
8 {
9 Log.Information("Starting up...");
10 CreateHostBuilder(args).Build().Run();
11 }
12 catch (Exception ex)
13 {
14 Log.Fatal(ex, "Application start-up failed");
15 }
16 finally
17 {
18 Log.CloseAndFlush();
19 }
20}
21
22public static IHostBuilder CreateHostBuilder(string[] args) =>
23 Host.CreateDefaultBuilder(args)
24 .UseSerilog()
25 .ConfigureWebHostDefaults(webBuilder =>
26 {
27 webBuilder.UseStartup<Startup>();
28 });
29}
That’s all you need to configure logging, all that is left is to add a few logging statements.
Here is a simple controller that lets you do division from, giving the opportunity to generate and log divide by zero exceptions.
1[ApiController]
2[Route("[controller]")]
3public class MathController : ControllerBase
4{
5 private readonly ILogger<MathController> _logger;
6
7 public MathController(ILogger<MathController> logger)
8 {
9 _logger = logger;
10 }
11
12 [HttpGet("division")]
13 public ActionResult Get(int a, int b)
14 {
15 _logger.LogInformation("Dividing {numerator} by {denominator}", a, b); // note how I can change the names of the attributes that DataDog logs to numerator and denominator.
16
17 int answer;
18 try
19 {
20 answer = a / b;
21 }
22 catch (DivideByZeroException ex)
23 {
24 _logger.LogError(ex, "SomeId: {someId}. ErrorCode: {errorCode} Division didn't work, was dividing {numerator} by {denominator}", Guid.NewGuid(), -99, a, b);
25
26 return BadRequest("Dividing by zero is not allowed");
27 }
28 _logger.LogInformation("Returning {result}", answer); // again the name of the attribute logged is different from the variable name in the code.
29 return Ok(answer);
30 }
31}
You can also log more complex objects in DataDog, like a Person with an id, first name, last name and date of birth. The full code for this is in the attached zip in the PersonController.cs
file, but it is as simple as -
1public ActionResult Post(Person person)
2{
3 _logger.LogDebug("Received new person: {@person}",person);
And here is what it looks like in DataDog.
A successful request -
And one that generates an exception -
And one that logs a complex object -
Now, there is a huge variety of things you can do within DataDog to search, alert, monitor, etc on the data but I’m not going into that here.
Full source code available here.