How to use LoggerMessage in ASP.NET Core

LoggerMessage features create cacheable delegates that require fewer object allocations and reduced computational overhead compared to logger extension methods, such as LogInformation and LogDebug. For high-performance logging scenarios, use the LoggerMessage pattern.


LoggerMessage provides the following performance advantages over Logger extension methods:

  • Logger extension methods require "boxing" (converting) value types, such as int, into object. The LoggerMessage pattern avoids boxing by using static Action fields and extension methods with strongly-typed parameters.

  • Logger extension methods must parse the message template (named format string) every time a log message is written. LoggerMessage only requires parsing a template once when the message is defined.


Create an ASP.NET Core 3.0 API project

Create an ASP.NET Core project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio.

  1. Launch the Visual Studio IDE.

  2. Click on “Create new project.”

  3. In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed.

  4. Click Next.

  5. In the “Configure your new project” window, specify the name and location for the new project.

  6. Click Create.