Table of Contents

Class MessageCategoryRegistry

Namespace
Arc4u.Diagnostics
Assembly
Arc4u.Diagnostics.dll

Gives a name to the categories invented by the application. A category is a short flag: the predefined ones are Technical (1), Business (2) and Monitoring (4); an application can use the other bits (8, 16, ...) for its own categories. Once a name is registered, the logger writes the Category property with that name and the Arc4u sinks, formatter and log stores read it back as the flag.

public static class MessageCategoryRegistry
Inheritance
MessageCategoryRegistry
Inherited Members

Examples

public static class SecurityLoggerExtensions
{
    public const MessageCategory SecurityCategory = (MessageCategory)8;

    static SecurityLoggerExtensions() => MessageCategoryRegistry.Register("Security", SecurityCategory);

    public static ILoggerWrapper<T> Security<T>(this ILogger<T> logger, [CallerMemberName] string methodName = "") =>
        logger.Category(SecurityCategory, methodName);
}

Remarks

Register the categories before the first entry is written, in every process that writes or reads them (a log reader in another process that does not know the name falls back to Technical). A static constructor on the class that holds the logger extension methods does it at the right time, see Category<T>(ILogger<T>, MessageCategory, string).

Methods

GetName(MessageCategory)

Gets the name of a category: its registered name, the name of a predefined category, or its number.

public static string GetName(MessageCategory category)

Parameters

category MessageCategory

The category.

Returns

string

The name written in the Category property.

Register(string, MessageCategory)

Registers the name of a category invented by the application. Registering the same name and value again does nothing.

public static void Register(string name, MessageCategory category)

Parameters

name string

The name written in the Category property (compared without regard to case).

category MessageCategory

The flag of the category: a positive value that is not one of the predefined MessageCategory values.

Exceptions

ArgumentException

name is empty, a number or the name of a predefined category; category is not positive or is a predefined value; or the name or the value is already registered with another value or name.

TryGetCategory(string?, out MessageCategory)

Gets the category of a name: a predefined name, a registered name or a number.

public static bool TryGetCategory(string? name, out MessageCategory category)

Parameters

name string

The name, compared without regard to case.

category MessageCategory

The category when the name is known.

Returns

bool

true when the name is a predefined name, a registered name or a positive number.