Skip to content
ConsoleMonster

API Reference

Class

ConsoleLogFile

Namespace ConsoleMonster.Output
Assembly ConsoleMonster
Modifiers sealed

Appends console text to a log file, as Write-Color's log option does: Text, Csv or JsonLines; appends under a file lock and retries with a growing pause, reporting each failed attempt to a warning callback.

Inheritance

  • Object
  • ConsoleLogFile

Constructors

public ConsoleLogFile(String path) #

Creates a log file writer.

Parameters

path System.String requiredposition: 0
The file path.

Methods

public Boolean Append(ConsoleLogRecord record) #
Returns: Boolean

Appends a record; returns false (after warnings) when every attempt failed.

Parameters

record ConsoleMonster.Output.ConsoleLogRecord requiredposition: 0
The record.

Returns

Whether the record was written.

public static Encoding? DetectEncoding(String path) #
Returns: Encoding?

The encoding of an existing file, from its byte order mark (UTF-8, UTF-16 LE/BE, UTF-32 LE/BE), or UTF-16 text without one (every other byte zero); null when the file is missing, empty or cannot be told (plain ASCII or UTF-8 without a mark, which ConsoleLogFile.Encoding handles).

Parameters

path System.String requiredposition: 0
The file path.

Returns

The detected encoding (writing no byte order mark of its own when appended), or null.

public String FormatRecord(ConsoleLogRecord record, Boolean isNewFile) #
Returns: String

The text appended for a record (including the CSV header for a new file).

Parameters

record ConsoleMonster.Output.ConsoleLogRecord requiredposition: 0
The record.
isNewFile System.Boolean requiredposition: 1
Whether the file is new or empty (CSV writes its header then).

Returns

The text.

public static Encoding ResolveEncoding(String? name) #
Returns: Encoding

An encoding by name. Write-Color's names are accepted; utf8 (the default) writes no byte order mark, utf8BOM does; unicode, string and unknown are UTF-16 LE (Out-File's "Unicode"); utf7 (obsolete) writes UTF-8.

Parameters

name System.String? requiredposition: 0
The name, or null for utf8.

Returns

The encoding.

Properties

public String Path { get; } #

The file path.

public ConsoleLogFormat Format { get; set; } #

The layout (default Text).

public String Separator { get; set; } #

The CSV separator (default a comma).

public Encoding Encoding { get; set; } #

The encoding for a new file (default UTF-8 without a byte order mark; Write-Color defaults to UTF-16 "Unicode"); see ConsoleLogFile.ResolveEncoding. An existing file keeps its own encoding (see ConsoleLogFile.KeepExistingEncoding).

public Boolean KeepExistingEncoding { get; set; } #

Whether appending to an existing file keeps that file's encoding (default true), detected by ConsoleLogFile.DetectEncoding, so a UTF-16 log written by Write-Color is never continued in UTF-8. A file whose encoding cannot be told (no byte order mark, not UTF-16 text) is appended to in ConsoleLogFile.Encoding.

public Boolean EncodingSpecified { get; set; } #

Whether ConsoleLogFile.Encoding was chosen by the caller (for example PowerShell's -Encoding). When it differs from an existing file's encoding, the file's encoding is kept and ConsoleLogFile.Warning is told once.

public Int32 Retries { get; set; } #

Attempts before giving up (default 2).

public Boolean IncludeTime { get; set; } #

Whether records start with the time (default true).

public String TimeFormat { get; set; } #

The time format (default yyyy-MM-dd HH:mm:ss).

public Action<String>? Warning { get; set; } #

Receives a message for each failed attempt (for example a PowerShell warning), or null.