Genocs.Logging

Genocs.Logging — Agent Reference Documentation

Consumer Mode for Agents

  • Assume package is installed from NuGet.
  • Do not rely on repository source code access.
  • Prefer stable public APIs and extension methods documented here.
  • If behavior is uncertain, fail safely and request config/package version details.

Purpose

Genocs.Logging adds Serilog-based host logging, sink configuration, optional correlation middleware, runtime log-level switching, and CQRS handler logging decorators.

Quick Facts

KeyValue
PackageGenocs.Logging
Target frameworksnet10.0, net9.0, net8.0
Primary roleStructured logging bootstrap and sink orchestration
Core entry pointsUseLogging, MapLogLevelHandler, AddCorrelationContextLogging, AddCommandHandlersLogging, AddEventHandlersLogging

Install

dotnet add package Genocs.Logging

Minimal Integration Recipe (Program.cs)

using Genocs.Logging;

StaticLogger.EnsureInitialized();

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseLogging();

var app = builder.Build();
app.MapLogLevelHandler("/logging/level");
app.Run();

Configuration

Use the logger section.

{
  "logger": {
    "enabled": true,
    "level": "Information",
    "otlpEndpoint": "http://localhost:4317",
    "minimumLevelOverrides": {
      "Microsoft": "Warning",
      "System": "Warning"
    },
    "excludePaths": ["/health", "/metrics"],
    "excludeProperties": ["RequestBody"],
    "tags": {
      "service": "orders-api",
      "team": "platform"
    },
    "console": {
      "enabled": true,
      "enableStructured": false,
      "enableTracing": true,
      "enableMetrics": false
    },
    "file": {
      "enabled": false,
      "path": "logs/app.log",
      "interval": "Day"
    },
    "seq": {
      "enabled": false,
      "url": "http://localhost:5341",
      "apiKey": ""
    },
    "httpPayload": {
      "enabled": false,
      "captureRequestBody": false,
      "captureResponseBody": true,
      "maxBodyLength": 4096,
      "allowedContentTypes": ["application/json"]
    }
  }
}
SettingTypeDescription
enabledboolEnables logging configuration through this section.
levelstringDefault minimum Serilog level.
otlpEndpointstringOTLP endpoint used by the Serilog OpenTelemetry sink.
minimumLevelOverridesobjectPer-source minimum level overrides.
excludePathsstring[]Request paths excluded from request logging.
excludePropertiesstring[]Log event properties removed before writing.
tagsobjectArbitrary enrichers added as static properties on each log event.
console.enabledboolEnables console sink output.
console.enableStructuredboolWrites structured JSON instead of plain text when enabled.
console.enableTracingboolEnables console tracing output when supported by the host configuration.
console.enableMetricsboolEnables console metrics output when supported.
file.enabledboolEnables local rolling-file logging.
file.pathstringLocal log file path.
file.intervalstringRolling interval understood by Serilog.Sinks.File.
elk.enabledboolEnables Elasticsearch sink output.
elk.basicAuthEnabledboolEnables HTTP basic auth for Elasticsearch.
elk.urlstringElasticsearch base URL.
elk.usernamestringElasticsearch user name.
elk.passwordstringElasticsearch password.
elk.indexFormatstringIndex naming pattern.
seq.enabledboolEnables Seq sink output.
seq.urlstringSeq server URL.
seq.apiKeystringSeq API key.
loki.enabledboolEnables Loki sink output.
loki.urlstringLoki push endpoint URL.
loki.batchPostingLimitintMaximum number of events per batch.
loki.queueLimitint?In-memory queue size before backpressure/drop behavior applies.
loki.periodTimeSpan?Delay between batch flushes.
loki.lokiUsernamestringOptional Loki user name.
loki.lokiPasswordstringOptional Loki password.
azure.enabledboolEnables Azure Application Insights sink output.
azure.connectionStringstringApplication Insights connection string.
mongo.enabledboolEnables MongoDB-oriented logging/tracing integration where supported.
httpPayload.enabledboolEnables request/response payload capture for logs and activity tags.
httpPayload.captureRequestBodyboolCaptures request bodies.
httpPayload.captureResponseBodyboolCaptures response bodies.
httpPayload.maxBodyLengthintMax captured payload size in characters.
httpPayload.allowedContentTypesstring[]Content types eligible for capture.

Keep httpPayload disabled unless you need deep diagnostics. It increases memory use and can expose sensitive data if you do not pair it with filtering and masking.

Decision Matrix For Agents

GoalPreferred API
Enable host loggingbuilder.Host.UseLogging()
Capture startup failures before host is builtStaticLogger.EnsureInitialized()
Expose runtime level switch endpointapp.MapLogLevelHandler("/logging/level")
Add correlation context logging middlewareAddCorrelationContextLogging() plus UseCorrelationContextLogging()
Add CQRS command/event decorator loggingAddCommandHandlersLogging() and AddEventHandlersLogging()

Behavior Notes / Constraints

  • Runtime level changes apply through a shared level switch.
  • Correlation logging middleware must be both registered and added to the pipeline.
  • CQRS decorator registration depends on handler assembly discovery.
  • Sink output depends on each configured sink enablement flag.

Public Capability Map

  • Host logging bootstrap through UseLogging.
  • Runtime log-level control through MapLogLevelHandler and ILoggingService.
  • Correlation middleware registration and pipeline integration.
  • CQRS command and event handler decorator logging.

Dependencies

  • Genocs.Core
  • Serilog packages
  • SmartFormat.NET

Troubleshooting

  1. Logs only appear in console. Fix: Enable and configure the target sink under logger.
  2. Runtime log-level endpoint returns an error response. Fix: Ensure builder.Host.UseLogging() is called before app build and keep MapLogLevelHandler mapped.
  3. CQRS decorator logs never appear. Fix: Register command and event logging decorators for the assembly that contains the handlers.