ASP.NET Core – Logging requests and responses

The simplest way to log requests/responses is to use the HTTP Logging middleware (added in v6). This is configurable, so you can make it suit your needs. If you need more control, you can add your own middleware instead. To use the HTTP Logging middleware, call UseHttpLogging() in your initialization code: Then add Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware to … Read more

Logging to the database with ASP.NET Core

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 Microsoft – No asynchronous logger methods … 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 it directly or fully integrate it 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. This logger gets added by default. There are two simple ways to get rid of these logging messages: I’ll show both options below. Option 1 – Turn off logging in appsettings.json … Read more

NLog – Log to console

There are two configuration options for logging to the console when you’re using NLog: In this article, I’ll show how to configure these two targets using nlog.config. At the end, I’ll show an example of configuring NLog programmatically. Console target Add a Console target and a rule that writes to that target in nlog.config, like … Read more

NLog – Archive by file size

To archive by file size when you’re using NLog, you can configure nlog.config like this: You specify archiveAboveSize in bytes. The above configuration is specifying ~1 MB. When your log file hits the specified size, NLog will “archive” the file, which really just means it will rename the log file and start a new log … Read more

NLog – Split trace logging into its own file

This article explains how to configure NLog so that trace-level log messages go to their own file. This is useful because trace logging involves logging every method call for short-term troubleshooting, leading to very large log files. This approach only requires modifying the nlog.config file, and doesn’t require any code changes. In the end, all … Read more

C# – Log every method call

I want to log method calls, including their parameter names and values, and what called the method. I want to minimize the amount of coding involved. For example: What options are available? In this article I’ll explain how to use the simple built-in approach. 1 – Create LogMethodCall() utility method The System.Diagnostics.StackFrame class gives us … Read more