C# – How to update appsettings.json programmatically

You have to overwrite the appsettings.json file to be able to update values programmatically. You have to deal with the whole file, not individual parts of it. The process can be summarized in the following steps: Load appsettings.json and deserialize it into an object. Update properties on the object. Serialize the object into a JSON … Read more

ASP.NET Core – Log messages to the database in the background

I was reading about logging in ASP.NET when I came across this statement about logging to the database: When logging to SQL Server, don’t do so directly. Instead, add log messages to an in-memory queue and have a background worker dequeue and insert data to SQL Server. Paraphrased from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#no-asynchronous-logger-methods In this article, I’ll show … Read more

How to use NLog in ASP.NET

When you want to use NLog in ASP.NET, the first step is to install and configure NLog. Then you can either use NLog directly or fully integrate NLog and ASP.NET. Use NLog directly if you prefer to have static ILogger properties, instead of using dependency injection. The downside of this approach is that you’ll have … Read more

ASP.NET Core – How to turn off startup logging

When you launch an ASP.NET Core web app, you may see the following startup logging messages: These messages come from having a console logger. If you’re using Host.CreateDefaultBuilder(), it adds a console logger provider by default. There are two simple ways to get rid of these logging messages: turn off logging in appsettings.json or remove … Read more

EF Core – How to create a database and a table

In this article, I’ll show how to use EF Core to create a database with one table in it. At the end, I’ll show a standalone console app that inserts a record into this database. Note: I’ll be using SQL Server. I haven’t tried this with other database providers. Please leave a comment if you’re … Read more

Adding your own feature flags in ASP.NET

In ASP.NET, you can add your own feature flags by using the feature management functionality. In this article, I’ll show how to configure and use feature management in ASP.NET and how to use this to feature gate an endpoint using your own feature flag. Configure feature management You need to do a few configuration steps … Read more

How to add User Secrets in a .NET Core console app

The User Secrets feature in .NET Core is a safe, simple way to override values in appsettings.json. The overridden values only exist in a file sitting in your own dev environment, so you don’t accidently commit them to your source control repository. This feature is enabled in ASP.NET by default, and the framework does most … Read more

ASP.NET – How to add User Secrets

To add User Secrets in an ASP.NET project in Visual Studio do the following steps: Right-click the project and click Manage User Secrets. This will create and open a file called secrets.json. Override appsettings.json settings by putting them in secrets.json For example, let’s say your appsettings.json looks like this: You want to override ApiKey. In … Read more