Chris Padilla/Blog
My passion project! Posts spanning music, art, software, books, and more
You can follow by Newsletter or RSS! (What's RSS?) Full archive here.
- Research and gather reference
- Study in context (breaking down the construction of the object)
- Practice (playing with the form, mixing and matching elements)
- Imitate
- Assimilate
- Innovate
- Read documentation/forum posts/books
- Build a few sample apps
- Stray off and build a more complex, custom app
Shapely Dogs
Dependency Injection and Logging in .NET
Buy in large, server logs come out of the box with .NET Core. Setting up logging for you controllers, however, takes just a little bit of setup.
In console applications, logging is straightforward:
System.Console.WriteLine("Hello World");
In a .NET application, though, you'll likely need to pipe your logs through the logger class from your Dependency Injection.
Here's a look at a bare bones Controller:
using Microsoft.AspNetCore.Mvc;
namespace Bookshelf_cs.Controllers;
using Bookshelf_cs.Data;
using Bookshelf_cs.Models;
public class AuthorController : ControllerBase
{
private readonly ILogger<AuthorController> _logger;
public AuthorController(ILogger<AuthorController> logger)
{
_logger = logger;
}
}
In the constructor, we're constructing our controller with a _logger
attribute.
An aside on Dependency Injection
So why is this better than just instantiating a logger class directly? Like so:
private readonly ILogger<AuthorController> _logger = new Logger();
The short answer is to avoid tight coupling. By using the ILogger
interface, you can swap out the default Microsoft logger with a custom logger so long as it shares the same interface. More details with examples on MSDN.
That swapping out happens at your app building stage in your Program.cs file. If we wanted to customize the logger, it would look like this:
// Program.cs
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
var app = builder.Build();
Very DRY. If you need to make changes, it's all done at the application level. From there, the builder injects your logger of choice into your classes that accept a logger in their constructors.
Logging
Once it's instantiated in your class, you can log directly from the _logger
attribute:
_logger.LogInformation(1001, "Author passed");
_logger.LogWarning(1001, obj.ToString());
For any custom changes for what level of logs are displayed, you can tweak the appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.AspNetCore.SpaProxy": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
How To Make (Anything) In 3 Steps
I came across Ctrl + Paint recently, another great and nearly free resource for learning to draw.
They have a fantastic video on how to draw anything in a few steps:
The steps are:
The glossed over step 0 is to understand the foundations: "How to render form and light." No small step! But, once you have the vocabulary in place, that's how you handle drawing something new from imagination.
A recurring theme on ol' Chris D Padilla dot com is that everything is the same! Here, for example, is how Clark Terry talks about learning to play and be creative in jazz:
And hey, here it is for developing in a new programming language:
The trick behind it, and why the title of Ctrl + Paints video skews just a smidge click-baity, is that each step takes time. The reward, though, is that when you're comfortable with this process, you literally can make just about anything. No need to know everything before starting a project, and no need to master everything right away. Just follow the cycle, and iterate from there.
Owls
Speer - Baroque Dance
So stately! π©