liveBook · Manning

Log

As explained in Building Web APIs with ASP.NET Core

Logging is the recording of application events and diagnostic information for later inspection.

A log is a chronological record of operations and events as an application runs. An entry can include when an event happened, its severity, its source, and a message. “Logs” can also mean the files holding those records. Logging sends this information to a secondary channel rather than the application’s normal response to its user. section 7.0 section 7.1 section 7.2.1

In a web API, logs help with several practical jobs:

  • Investigate bugs and unhandled exceptions so they can be fixed promptly. section 7.1.2
  • Identify suspicious activity or abnormal behavior, including problems that may need an alert or automated response. section 7.1.2
  • Keep records needed for security and compliance policies. section 7.1.2

Log a board-game request

In the board-game API’s Get action, `_logger.LogInformation("Get method started.")` records an event when the action starts. Run the API in Debug mode, call the endpoint, and check Visual Studio’s Output window. The resulting line identifies the controller, the Information level, and the message. Other components, including Entity Framework Core, can write through the same logging API. section 7.2.1

Choose a level to describe each event’s severity: the ILogger shortcuts include LogTrace, LogDebug, LogInformation, LogWarning, LogError, and LogCritical. An exception handler can call LogError with the exception and an event ID, making related error entries easier to filter. section 7.2.1 section 7.2.6

A text log file is useful for inspection, but records stored as predefined fields can be queried without parsing messages. Structured logging can, for example, keep events in a SQL Server LogEvents table. Serilog can add columns such as SourceContext and direct events to more than one destination. section 7.3.2 section 7.4.4 section 7.4.5

builder.Host.UseSerilog((ctx, lc) => {
    lc.ReadFrom.Configuration(ctx.Configuration);
    lc.Enrich.WithMachineName();
    lc.Enrich.WithThreadId();
lc.WriteTo.File("Logs/log.txt",            #1
rollingInterval: RollingInterval.Day);#2
    
    // ... non-relevant parts omitted ...

This Serilog configuration writes to `Logs/log.txt` and starts a new file each day. Trigger an API request, then inspect the resulting log file to confirm that events were recorded. section 7.4.5

Keep the destination and level appropriate to the job. Serilog can set minimum levels for the application and its components, restrict a particular sink to a level, and format file entries with a timestamp, level, message, and exception. section 7.4.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines logs and works through controller entries, severity levels, structured storage, file output, and verification.
Examples
Uses a board-game controller message, a Visual Studio Output entry, a LogEvents table, and a daily rolling file.
What it assumes
Assumes an ASP.NET Core web API with a controller while defining logging from first principles.
What you can do after
Operate/configure it by writing ILogger entries, setting Serilog destinations and levels, and checking the recorded events.
How it connects
Connects logs to ILogger, exception handling, structured logging, SQL Server, and monitoring.

Also explained in

Compare how 14 books explain log →

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.