Skip to content

Logging and Diagnostics

Mike Christensen edited this page Aug 28, 2026 · 2 revisions

Logging and diagnostics

KitchenPC Core and DB use Microsoft.Extensions.Logging, the standard logging abstraction for modern .NET. The libraries select categories and levels but never choose a destination. Logging is optional; contexts use NullLoggerFactory unless an application supplies a factory.

Console application

Add a provider package such as Microsoft.Extensions.Logging.Console, create a factory for the application lifetime, and pass it to the context builder before initialization:

using KitchenPC.Core;
using KitchenPC.Core.Context;
using Microsoft.Extensions.Logging;

using var loggerFactory = LoggerFactory.Create(logging =>
{
   logging.SetMinimumLevel(LogLevel.Warning);
   logging.AddSimpleConsole(options => options.SingleLine = true);
   logging.AddFilter("KitchenPC", LogLevel.Information);
});

var context = StaticContext.Configure
   .DataDirectory(dataDirectory)
   .Logging(loggerFactory)
   .Identity(() => AuthIdentity.Anonymous)
   .Create();

context.Initialize();

DBContext.Configure.Logging uses the same pattern. Its logger factory flows to the database adapter, importer, exporter, parser, and modeler. DatabaseAdapter.Configure.Logging is also available when an adapter is used directly for provisioning before it belongs to a context.

ASP.NET Core

ASP.NET Core already configures Microsoft logging. If context-initialization logs are required, create or retain an ILoggerFactory for the application lifetime and pass it through .Logging(loggerFactory) while building the root context. Send output to the providers appropriate for the host—console, OpenTelemetry, Application Insights, or another provider—without changing KitchenPC.

Useful KitchenPC categories include:

  • KitchenPC.Core.NLP.Parser for parsing and synonym diagnostics;
  • KitchenPC.Core.Modeler.DBSnapshot and KitchenPC.Core.Modeler.ModelingSession for modeler timing;
  • KitchenPC.DB.DatabaseImporter and KitchenPC.DB.DatabaseExporter for provisioning counts.

NHibernate and Npgsql maintain their own categories. Start at Warning, temporarily raise only the category being diagnosed, and never log connection strings or sensitive user-entered recipe data.

NLP trace

Context initialization connects the built-in NLP trace to the configured logger factory. Existing applications can also choose a tracer explicitly:

using KitchenPC.Core.NLP;

NlpTracer.SetTracer(new DefaultTracer(loggerFactory));
var result = context.ParseIngredientUsage("a dozen bananas");

For another destination or trace representation, implement ITracer and pass it to SetTracer. The interface receives a trace level, composite-format string, and arguments. NLP tracing is global and can be extremely verbose, so configure it once and use detailed output only for focused diagnostics.

What to capture

Useful operational signals include:

  • context initialization duration and selected capabilities;
  • recipe search duration, result count, and sanitized filter summary;
  • NLP status counts without raw personal data;
  • provisioning/import phase and record counts;
  • missing-capability and incompatible-amount exceptions;
  • database connectivity and timeout failures.

Diagnosing a parse

  1. Preserve the original input and Result.Status in a safe development environment.
  2. Enable the KitchenPC.Core.NLP.Parser category or install a focused ITracer.
  3. Check ingredient and synonym presence.
  4. Check the default form pairing for the parsed unit type.
  5. Check form/unit compatibility and custom-unit mappings.
  6. Improve source metadata instead of hard-coding UI-only exceptions when the rule is generally valid.

Clone this wiki locally