Repository navigation
Host-facing lifecycle control:
LOG.Flush()/LOG.Shutdown()and their async counterparts, plus log files you can actually open while the process is running. Everything here is additive — no existing signature changed, no default value changed — so upgrading from 3.2.0 needs no code changes.The reason this matters: logging is asynchronous, so until now there was no way to ask "is what I wrote actually on disk?" other than sleeping and hoping. Hosts that are about to be killed (container stop, crash handler, an app-wide
finally) had no barrier to wait on, and anything that shut the logger down had no way to stop the background worker.
Added
LOG.Flush() — a real barrier, not a sleep
LOG.Flush()/LOG.Flush(int timeoutMs)blocks until every entry written so far has reached the disk, then returnstrue. Returnsfalseon timeout (default 10 s) or when the logger is already shut down.- The calling thread helps drain the queue rather than waiting out the dispatcher's
FlushIntervalMs, so the guarantee is exact: write 1000 entries, callFlush(), and the file has 1000 lines by the time it returns — no sleep, no re-read, no retry loop. LOG.FlushAsync(CancellationToken)/LOG.FlushAsync(int timeoutMs, CancellationToken)returnTask<bool>on every target framework,netstandard2.0included. A cancelled token returnsfalserather than throwingOperationCanceledException.- Both pipelines (main logger and Quote) are flushed, and both force an
fsync— the reason a host callsFlushis that the process may be killed next, so handing the buffer to the OS is not enough.
LOG.Shutdown() — stop the background work
LOG.Shutdown()/LOG.Shutdown(int timeoutMs)/LOG.ShutdownAsync(...): flush, then stop the dispatcher, the periodic disk-flush timer and the retention-cleanup timer, then close every open log file.- Idempotent. The first call returns
true, later calls returnfalse— that is a report, not an error. It also interleaves safely with the built-inProcessExit/UnhandledExceptioncleanup in either order, so a host that callsShutdown()explicitly and then exits normally does not double-run the cleanup or trip over half-closed streams. - Writes after
Shutdownare silently discarded — no exception, not even a console line. A background-logging library must not take its host down on the way out, and a host that is already shutting down should not have to guard every log statement.LOG.IsShutdownreports the current state.
Configure after Shutdown
LOG.Configure(...)remains non-reentrant while the pipeline is alive (unchanged v3.0 behavior — it exists so nobody swaps settings mid-run), but it is now allowed again afterShutdown: it restarts the pipeline and clears the discard state. That restriction only ever made sense while something was running.- The options are reset to defaults on restart, so the previous round's settings cannot linger as invisible state that nobody configured and nobody can see.
Fixed
Log files could not be opened by external tools while running
- Files were opened with
FileShare.Read, which only admits readers that open the file read-only.tail, editors and most log viewers open withFileAccess.ReadWriteand were rejected with a sharing violation — in practice, you could not watch the log while the process was running, which is most of the point of a local file logger. - Files are now opened with
FileShare.ReadWrite(both the main logger and the Quote pipeline). The writer keeps its append-only handle; this only widens what other processes are permitted to do with the file.
Technical
- Removed the
DocumentationFileproperty from the project file. It pinned all five target frameworks to a singlefile.xmlin the project directory (five builds racing to write the same file) and shipped the docs aslib/<tfm>/file.xml— a name IntelliSense ignores, since it only looks for<AssemblyName>.xml. The bilingual XML documentation was invisible to consumers all along. With onlyGenerateDocumentationFileleft, each framework generates its ownOzaLog.xmland the package now carrieslib/<tfm>/OzaLog.xml, which IntelliSense picks up. The stalefile.xmlchecked into the repository has been removed. - Both pipelines now track outstanding entries with a pending counter instead of inferring completion from an empty queue. An empty queue does not mean the work is done — the dispatcher may have dequeued an item that has not reached the file yet — and a
Flushthat trusted the queue would return one entry early. The counter is incremented before the enqueue for the same reason. - The dispatcher's semaphore and
CancellationTokenSourceare rebuilt on eachInitialize, so the pipeline can genuinely restart afterShutdown; theProcessExit/UnhandledExceptionhandlers are registered once per process, so a restart does not stack up duplicate cleanup handlers. FileStreamPool.FlushAllandQuoteFileStreamPool.FlushAllgained aflushToDiskoverload (the no-argument version keeps its existing behavior). The periodic timer still passesfalse— hand the buffer to the OS, throughput first;Flush/Shutdownpasstrueand force thefsync.- New internal coordinator
LogLifecycle— the single place that owns the shutdown flag and sequences both pipelines, soLOGitself stays a thin facade. - New xUnit tests:
LifecycleTests(a 1000-entry burst is exactly 1000 lines whenFlushreturns; writes afterShutdownthrow nothing and leave no trace in any file;Shutdownis idempotent and survives theProcessExitcleanup running afterwards;Configurestays non-reentrant untilShutdown; logging resumes after a restart) andFileShareTests(the file is readable and read-write openable while the writer holds it, and the writer keeps working afterwards). Cross-class test parallelization is disabled at assembly level, because shutting the pipeline down is process-global state and would otherwise cut other test classes off mid-write. 81 tests green (73 in 3.2.0). - Zero NuGet dependencies on
net8.0/net9.0/net10.0unchanged; build verified across all five target frameworks with 0 errors and 0 warnings.
Known limitations
- Entries written within the same millisecond are not guaranteed to land in file order. The timestamp cache has 1 ms resolution and the queue is drained in batches. Use
HighPrecisionTimestamp = trueif you need to reorder entries after the fact. Not planned to change: the 1 ms cache is what keeps the caller-side cost at a few nanoseconds. - Synchronous mode (
EnableAsyncLogging = false) still has no retention cleanup —KeepDaysis enforced by a background cleaner that only the async pipeline starts. Carried over from 3.2.0.