Table of Contents

Class VhLogger

Namespace
VpnHood.Net.Toolkit.Logging
Assembly
VpnHood.Net.Toolkit.dll

Provides the process's one log, and the formatting that keeps identifying values out of it.

public static class VhLogger
Inheritance
VhLogger
Inherited Members

Remarks

Its sinks are providers, added once by whoever owns them, before anything logs: an app's console, a service's system log, a file. MinLogLevel is the log's own filter, read at each line, so no sink is rebuilt when the app's log settings change it; a sink may refuse more. Redaction applies to the values passed through the Format methods, not to messages.

Properties

Instance

Gets the process's logger, the same object for the process's life.

public static ILogger Instance { get; }

Property Value

ILogger

IsAnonymousMode

Gets or sets a value indicating whether Format redacts identifying values: addresses, endpoints, networks, IDs and host names. The default is true.

public static bool IsAnonymousMode { get; set; }

Property Value

bool

MinLogLevel

Gets or sets the lowest level the log writes. The default is Information.

public static LogLevel MinLogLevel { get; set; }

Property Value

LogLevel

Redactor

The redactor this logger applies to the values passed through Format. Set only through IsAnonymousMode, so that turning redaction off stays a single deliberate switch rather than something any caller can install. Callers that must redact whatever the logging settings say use Always instead of this one.

public static Redactor Redactor { get; }

Property Value

Redactor

TcpCloseEventId

Gets or sets the event ID of the Debug line LogError(EventId, Exception, string, params object?[]) writes for a socket's ordinary close.

public static EventId TcpCloseEventId { get; set; }

Property Value

EventId

Methods

AddProvider(ILoggerProvider)

Adds a sink to the log.

public static void AddProvider(ILoggerProvider loggerProvider)

Parameters

loggerProvider ILoggerProvider

The sink's provider, which the log keeps for the process's life.

Exceptions

ArgumentNullException

loggerProvider is null.

Format(IEnumerable<IPAddress>)

Wraps IP addresses for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<IReadOnlyList<IPAddress>> Format(IEnumerable<IPAddress> ipAddresses)

Parameters

ipAddresses IEnumerable<IPAddress>

The addresses.

Returns

Redactor.RedactedValue<IReadOnlyList<IPAddress>>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Format(IEnumerable<IpNetwork>)

Wraps networks for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<IReadOnlyList<IpNetwork>> Format(IEnumerable<IpNetwork> ipNetworks)

Parameters

ipNetworks IEnumerable<IpNetwork>

The networks.

Returns

Redactor.RedactedValue<IReadOnlyList<IpNetwork>>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Format(EndPoint?)

Wraps an endpoint for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<EndPoint> Format(EndPoint? endPoint)

Parameters

endPoint EndPoint

The endpoint, or null.

Returns

Redactor.RedactedValue<EndPoint>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Format(IPAddress?)

Wraps an IP address for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<IPAddress> Format(IPAddress? ipAddress)

Parameters

ipAddress IPAddress

The address, or null.

Returns

Redactor.RedactedValue<IPAddress>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Format(IPEndPoint?)

Wraps an endpoint for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<EndPoint> Format(IPEndPoint? endPoint)

Parameters

endPoint IPEndPoint

The endpoint, or null.

Returns

Redactor.RedactedValue<EndPoint>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Format(IpEndPointValue?)

Wraps an endpoint for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<EndPoint> Format(IpEndPointValue? endPoint)

Parameters

endPoint IpEndPointValue?

The endpoint, or null.

Returns

Redactor.RedactedValue<EndPoint>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

Exceptions

InvalidOperationException

The endpoint is the default value, which holds no address.

ArgumentOutOfRangeException

The endpoint's port is out of range.

Format(IpNetwork?)

Wraps a network for a log line, redacted by the current redactor when the line turns it into text.

public static Redactor.RedactedValue<IpNetwork> Format(IpNetwork? ipNetwork)

Parameters

ipNetwork IpNetwork

The network, or null.

Returns

Redactor.RedactedValue<IpNetwork>

A wrapper that formats on each ToString, with the redactor captured by this call.

Remarks

In anonymous mode a globally routable address becomes a token, stable within the run; a private or local address, a port, a prefix length and an endpoint that is not an IP one stay as they are, and several values are joined by commas. A conversion or a copy of a sequence happens now; the redaction each time the text is made.

FormatHostName(string?)

Formats a host name for the log: a name that is an address or an endpoint is redacted as one; another, in anonymous mode, shows only its last four characters, and a name over eight characters its first two too.

public static string FormatHostName(string? dnsName)

Parameters

dnsName string

The host name, or null.

Returns

string

The text.

FormatHostName(string?, int)

Formats a host name and a port for the log, as host:port; the host as FormatHostName(string?) does.

public static string FormatHostName(string? dnsName, int port)

Parameters

dnsName string

The host name, or null.

port int

The port.

Returns

string

The text.

FormatId(object?)

Formats an identifying ID for the log. In anonymous mode only its second half shows, after **.

public static string FormatId(object? id)

Parameters

id object

The ID, or null.

Returns

string

The text.

FormatIpPacket(string)

Formats a packet's text for the log. In anonymous mode the addresses after its source and destination labels are redacted as Format redacts an address.

public static string FormatIpPacket(string ipPacketText)

Parameters

ipPacketText string

The packet's text.

Returns

string

The text.

FormatSessionId(object?)

Formats a session ID for the log, as it is: it is not redacted.

public static string FormatSessionId(object? id)

Parameters

id object

The ID, or null.

Returns

string

The ID's text, or <null>.

FormatType(object?)

Formats an object's type for the log: its name.

public static string FormatType(object? obj)

Parameters

obj object

The object, or null.

Returns

string

The type's name, or <null>.

FormatType<T>()

Formats a type for the log: its name.

public static string FormatType<T>()

Returns

string

The type's name.

Type Parameters

T

The type.

IsSocketCloseException(Exception)

Determines whether an exception, or one along its InnerException chain, is a disposal, a cancellation, or a socket error of an aborted, reset or refused connection, an aborted operation or a network reset, wherever it comes from.

public static bool IsSocketCloseException(Exception ex)

Parameters

ex Exception

The exception.

Returns

bool

true if it is; otherwise, false.

LogError(EventId, Exception, string, params object?[])

Logs an error, or, for what IsSocketCloseException(Exception) accepts, a Debug line with TcpCloseEventId and the outer exception's message, without the exception.

public static void LogError(EventId eventId, Exception ex, string message, params object?[] args)

Parameters

eventId EventId

The error's event ID.

ex Exception

The exception.

message string

The message template.

args object[]

The template's arguments.