Oakrey.Applications.Trace
2.0.3
dotnet add package Oakrey.Applications.Trace --version 2.0.3
NuGet\Install-Package Oakrey.Applications.Trace -Version 2.0.3
<PackageReference Include="Oakrey.Applications.Trace" Version="2.0.3" />
<PackageVersion Include="Oakrey.Applications.Trace" Version="2.0.3" />
<PackageReference Include="Oakrey.Applications.Trace" />
paket add Oakrey.Applications.Trace --version 2.0.3
#r "nuget: Oakrey.Applications.Trace, 2.0.3"
#:package Oakrey.Applications.Trace@2.0.3
#addin nuget:?package=Oakrey.Applications.Trace&version=2.0.3
#tool nuget:?package=Oakrey.Applications.Trace&version=2.0.3
Oakrey.Applications.Trace
A .NET library for tracing application activities to files. Exposes a base class and interface for building custom tracers with start/stop control, real-time Rx observables for status and elapsed time, configurable file naming, and thread-safe file writing.
NuGet package ID: Oakrey.Applications.Trace
Main features
ITracerinterface withStartTrace/StopTrace,Tracingstate, andTracePathconfigurationTracerBaseabstract base class handles all file I/O, timer management, and observable publishingIObservable<TracerStatus>(Stopped / Running / Paused) emitted viaStatusChangedIObservable<TimeSpan>emitted every millisecond viaTraceTimeChangedfor live elapsed-time binding- Configurable trace folder path, file naming policy, file increment counter, and format string via
ITracerSettings - File name resolution delegated to
Oakrey.Files.FileNameSource(supports naming policies and auto-increment) - Thread-safe line writing using
lockandMemoryPool<byte>to avoid per-write allocations - Automatic trace-file header and footer written by the framework
- Integrated with
Oakrey.Logfor internal diagnostic logging andOakrey.Telemetryfor activity tracing IDisposablesupport: stopping an active trace on disposal
Architecture
classDiagram
class ITracer {
+IObservable~TracerStatus~ StatusChanged
+IObservable~TimeSpan~ TraceTimeChanged
+bool Enabled
+bool FolderTraceCorrect
+bool Tracing
+string TracePath
+string TraceTime
+StartTrace()
+StopTrace()
}
class TracerBase {
#abstract string FileName
#abstract string SwName
#abstract string Extension
#abstract WriteHeader()
#WriteLine(string)
#int Incrementor
}
class ITracerSettings {
+string LastTraceFolderPath
+FileNamingPolicy TraceFileNamingPolicy
+int LastTraceFileIncrement
+string Format
}
class TracerStatus {
<<enumeration>>
Stopped
Running
Paused
}
ITracer <|.. TracerBase
TracerBase --> ITracerSettings
TracerBase --> TracerStatus
Project files
| File | Purpose |
|---|---|
ITracer.cs |
Public contract for all tracer implementations |
TracerBase.cs |
Abstract base with file I/O, timer, and observable wiring |
ITracerSettings.cs |
Settings contract injected into TracerBase |
TracerStatus.cs |
Enum: Stopped, Running, Paused |
Requirements
- .NET 10 or higher
Oakrey.Files3.0.0Oakrey.Log2.0.0Oakrey.Telemetry2.0.1System.Reactive6.1.0
Installation
NuGet Package Manager
- Open your project in Visual Studio.
- Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for
Oakrey.Applications.Traceand click Install.
.NET CLI
dotnet add package Oakrey.Applications.Trace
Package Manager Console
Install-Package Oakrey.Applications.Trace
Configuration
Implement ITracerSettings to provide trace configuration. All properties must be persisted by the caller (e.g. in user settings or a configuration file):
public class MyTracerSettings : ITracerSettings
{
public string LastTraceFolderPath { get; set; } = @"C:\Traces";
public FileNamingPolicy TraceFileNamingPolicy { get; set; } = FileNamingPolicy.DateTimePrefix;
public int LastTraceFileIncrement { get; set; } = 0;
public string Format { get; set; } = "yyyy-MM-dd";
}
Example usage
Extend TracerBase to define the file name, application name, extension, and the custom header written at the top of every trace file:
public class AppTracer : TracerBase, ITracer
{
public AppTracer(ITracerSettings settings) : base(settings) { }
protected override string FileName => "AppTrace";
protected override string SwName => "MyApplication";
protected override string Extension => "log";
protected override void WriteHeader()
{
WriteLine("=== MyApplication trace ===");
WriteLine($"Started: {DateTime.Now}");
}
// Expose StartTrace publicly (protected in base)
public new void StartTrace() => base.StartTrace();
}
Subscribe to observables to drive UI or diagnostic output:
AppTracer tracer = new(new MyTracerSettings());
tracer.StatusChanged.Subscribe(status =>
Console.WriteLine($"Tracer status: {status}"));
tracer.TraceTimeChanged.Subscribe(elapsed =>
Console.WriteLine($"Elapsed: {elapsed}"));
tracer.StartTrace();
// ... write lines via WriteLine inside the tracer
tracer.StopTrace();
Development notes
TracerBaseacquires aLockon everyWriteLinecall. Writers on background threads are safe without additional synchronization.- The elapsed-time timer fires every 1 ms. Subscribers on the UI thread should observe on the dispatcher scheduler to avoid cross-thread issues.
Pausedis defined inTracerStatusbut is not set byTracerBaseitself. Custom subclasses can publish it via the inheritedstatussubject.- Disposal calls
StopTraceif tracing is active, ensuring the trace footer is written and the file is closed cleanly.
License
MIT - Copyright (c) Oakrey 2016-present
Contributions are welcome! Feel free to open issues or submit pull requests to improve the package.
License
This project is licensed under the MIT License. See the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Oakrey.Files (>= 3.0.0)
- Oakrey.Log (>= 2.0.0)
- Oakrey.Telemetry (>= 2.0.1)
- System.Reactive (>= 6.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.