---
title: Console log service discovery
summary: Discovers machine-scoped numbered log-file environment variables and registers a log writer per match.
status: draft
owner: <owner>
last_reviewed: 2026-09-15
library: SAIF.Framework.Windows
reference_type: package
operation_id: OP-WIN-006
trigger: Autofac resolution of IConsoleLogService, registered by dependency and unit-of-work registration.
---

# Console log service discovery

`ConsoleLogService`'s constructor discovers machine-scoped OS environment variables that identify numbered log files and registers a `LogWriter` for each match, in addition to console output used throughout the [console application lifecycle](console-base-run.md).

| Field | Value |
| --- | --- |
| **Operation ID** | OP-WIN-006 |
| **Trigger** | Autofac resolution of `IConsoleLogService` (registered by [dependency and unit-of-work registration](register-common-dependencies.md)). |
| **Inputs** | Machine-scoped environment variables (`Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)`). |
| **Outputs** | A populated `loggers` list of `LogWriter` instances. |
| **Primary components** | `ConsoleLogService` constructor, `RegisterLogFile`, `LogWriter` constructor |
| **Entry point** | `Console\SAIF.Console\Console\ConsoleLogService.cs` |

## Steps

1. Filter machine environment variable names matching regex `^file[0-4]` (case-insensitive, lower-cased before matching).
2. For each matching variable name, call `RegisterLogFile(key, Environment.GetEnvironmentVariable(key))`, which adds `new LogWriter(logId, filePath)` to the `loggers` list.
   - `LogWriter`'s constructor maps `logId.ToLower()` to a `LogType` (`file0`–`file4`, else `Custom`) via a fixed lookup dictionary ([BR-WIN-014](../business-rules.md#br-win-014-log-file-environment-variables-map-to-fixed-log-types)).
3. No duplicate-path check is performed despite a commented-out `//if (loggers.Any(filepath)` line in source — this inactive check is not documented as active behavior; duplicate machine env vars pointing at the same file path are not deduplicated.
4. `LogWriter`'s underlying `StreamWriter` is lazily created on first `Write`/`WriteLine`/`WriteTimedLine` access (`if (writer.IsNull()) writer = new StreamWriter(FilePath)`), not at construction time.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([ConsoleLogService constructed]) --> GetVars[Get machine environment variables]
	GetVars --> Filter[Filter names matching file0-4]
	Filter --> Loop[For each match]
	Loop --> Register[RegisterLogFile: new LogWriter]
	Register --> MapType[Map name to LogType, default Custom]
	MapType --> Loop
	Loop --> Done([loggers list populated])
```

## Failure and edge-case behavior
- Duplicate machine environment variables pointing at the same file path each produce a separate `LogWriter` instance; no deduplication occurs.
- `Flush()` on `ConsoleLogService` and `LogWriter` throws `NotImplementedException` unconditionally — this is confirmed as-implemented, but whether any consuming application calls these members (which would surface as a runtime defect) is unknown.**
- The underlying `StreamWriter` for a given `LogWriter` is not created until the first log write, so a registered log file that is never written to produces no file on disk.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

> **Note:** Statements marked with `**` could not be confirmed; treat as unknown until verified.

## Sources
- `Console\SAIF.Console\Console\ConsoleLogService.cs`
- `Console\SAIF.Console\Console\LogWriter.cs`
