liveBook · Manning

Event id

As explained in Building Web APIs with ASP.NET Core

EventId is logging metadata that identifies the event associated with a log record and can contain values of different types.

An event ID is logging metadata that classifies related log records. A controller action or exception can receive a custom number, allowing records to be grouped, filtered, or audited. section 7.2.5 section 7.2.6

A practical implementation uses the logging API and a centralized convention:

  • Pass the event ID as the first parameter to LogInformation or LogError. section 7.2.5 section 7.2.6
  • Define constants such as BoardGamesController_Get = 50110 and Error_Get = 50001 in a shared CustomLogEvents class. section 7.2.5 section 7.2.6
  • Verify the result in the console, remembering that providers differ in whether and where they display the ID. section 7.2.5

Classifying a controller log

Calling _logger.LogInformation(CustomLogEvents.BoardGamesController_Get, "Get method started.") produces a record associated with event 50110. The constant makes the convention readable and reduces mistyped numeric literals.

Listing 7.1 CustomLogEvents class
namespace MyBGList.Constants
{
   public class CustomLogEvents
   {
       public const int BoardGamesController_Get = 50110;
       public const int BoardGamesController_Post = 50120;
       public const int BoardGamesController_Put = 50130;
       public const int BoardGamesController_Delete = 50140;

       public const int DomainsController_Get = 50210;
       public const int DomainsController_Post = 50220;
       public const int DomainsController_Put = 50230;
       public const int DomainsController_Delete = 50240;

       public const int MechanicsController_Get = 50310;
       public const int MechanicsController_Post = 50320;
       public const int MechanicsController_Put = 50330;
       public const int MechanicsController_Delete = 50340;
   }
}

Browse a complete event-ID naming scheme covering controller operations and their corresponding numeric constants. section 7.2.5

Structured providers may store EventId inside a semistructured Properties value. Because EventId is complex and can contain values such as integers or strings, Serilog can map it to a dedicated database column when configured. section 7.4.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines event IDs, demonstrates logging overloads and centralized constants, then shows provider-specific display and structured storage.
Examples
Uses controller-operation constants, an exception identifier, console output, and a Serilog Properties entry containing EventId.
What it assumes
Assumes familiarity with ILogger calls and introduces event IDs through logging-method overloads and shared constants.
What you can do after
Operate/configure application logging with custom identifiers and inspect how providers display or persist those identifiers.
How it connects
Connects event IDs with application logging, exception logging, structured logging, message templates, and Serilog.

Also explained in

Compare how 2 books explain event id →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.