JSON Logging
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.
- Add Serilog.AspNetCore to your projectbash
dotnet add package Serilog.AspNetCore
- Configure the logger in your
Program.cs
csharppublic 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/"); });
- Enable Request Logging in your
Startup.cs
csharppublic 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
- Add logging to one of your implementation classes be injecting it using an
ILogger
interfacecsharppublic 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!!!!
- Use your logger instance wherever appropriatecsharp
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; }