liveBook · Manning

SourceContext

As explained in Building Web APIs with ASP.NET Core

SourceContext is Serilog log-event metadata containing the namespace or category of the class that originated the record.

SourceContext is Serilog metadata that records the namespace or category of the class that originated a log event. Serilog stores it in the Properties column by default. section 7.4.5

Store SourceContext in SQL

Add a ColumnOptions collection with a SqlColumn whose ColumnName and PropertyName are both SourceContext and whose data type is NVarChar. Delete the existing LogEvents table, run the application, visit /BoardGame, and verify the regenerated table in SSMS. section 7.4.5

<propertykey='EventId'>              #1
    <structure type=''>
      <property key='Id'>
        50110 #2
      </property>
    </structure>
  </property>
  <property key='SourceContext'> #1
    MyBGList.Controllers.BoardGamesController#2
  </property>

A stored log event shows SourceContext as a simple property containing the originating controller namespace. section 7.4.5

Use SourceContext to:

  • Identify which application namespace produced a record. section 7.4.5
  • Filter or inspect application records separately from Microsoft.AspNetCore records through logging-level configuration. section 7.4.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the property’s meaning, default XML storage, dedicated-column mapping, table regeneration, and verification, but not its internal generation.
Examples
Shows XML properties, MyBGList.Controllers.BoardGamesController, ColumnOptions configuration, SSMS inspection, and the /BoardGame endpoint.
What it assumes
Assumes a Serilog-configured application writing LogEvents to SQL Server and introduces dedicated-column mapping through ColumnOptions.
What you can do after
Operate/configure Serilog to persist SourceContext in a dedicated SQL column and verify originating namespaces in SSMS.
How it connects
Connects SourceContext with Serilog, EventId, structured log properties, SQL Server sinks, and logging-level overrides.

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.