Facility for logging

MappingGenerator Premium provides a set of scaffoldings that facilitate the usage of Microsoft.Extensions.Logging.ILogger.

Add logger access

This premium code action automatically injects a strongly typed logger. It works as follows:

  • Add using for Microsoft.Extensions.Logging namespace if necessary.
  • Add ILogger<T> private, readonly field if necessary.
  • Inject ILogger<T> instance through the existing constructors or adds a new constructor if necessary.
  • Insert logger field accessor.

Log (severity)

Automatically inject logger (if needed) and log text with the desired severity.

Log exception

This premium code action works in a similar way as Add logger but additionally insert invocation of LogError method with exception from the current scope. If catch section is missing, then code action adds it automatically:

Catch and Log exception

Wrap the selected code block with try..catch statement, automatically inject the logger (if needed) and log the caught exception.

Wrap in logging scope

Wrap the selected code block with using statement for ILogger.BeginScope. If there’s no field with logger, then inject it automatically.