# Video

# Logging Best Practices

In order to work best with log aggregators like EFK Stack or Splunk, you want to output your logs in JSON format for easier indexing.

  1. Add Serilog.AspNetCore (opens new window) to your project
    dotnet add package Serilog.AspNetCore
    
  2. Configure the logger in your Program.cs
    public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args
             .UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(hostingContext.Configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console(new RenderedCompactJsonFormatter()))
             .ConfigureWebHostDefaults(webBuilder =>
             {
                webBuilder.UseStartup<Startup>()
                         .UseUrls("http://0.0.0.0:8080/");
             });
    
  3. Enable Request Logging in your Startup.cs
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
       if (env.IsDevelopment())
       {
             app.UseDeveloperExceptionPage();
       }
       else
       {
             app.UseExceptionHandler("/Home/Error");
       }
    
       app.UseSerilogRequestLogging(); // <-- Add this line
    
       // Other app configuration
    
    • This will ensure that all requests handled by the ASP.NET WebAPI Router will be logged
  4. Add logging to one of your implementation classes be injecting it using an ILogger interface
    public class TodoListContext : DbContext, ITodoContext
    {
       private readonly ILogger<TodoListContext> _logger;
    
       public TodoListContext(ILogger<TodoListContext> logger, DbContextOptions<TodoListContext> options) :
          base(options)
       {
          _logger = logger;
       }
    
    • NOTE - You will need to also update your Unit Test to make the signature match the updated constructor here!!!!
  5. Use your logger instance wherever appropriate
    public Todo UpdateTodo(Guid id, Todo data)
    {
       var todo = Todos.Find(id);
       if (todo == null)
       {
             var ae = new ArgumentException($"Unable to find Todo with ID: ${id}");
             _logger.LogWarning(ae, "Unable to find Todo with Id {id}", id);
             throw ae;
       }
       todo.Complete = data.Complete;
       todo.Description = data.Description;
       todo.Title = data.Title;
       todo.DueDate = data.DueDate;
       SaveChanges();
       return todo;
    }
    
Last Updated: 12/18/2021, 5:53:27 PM