Skip to main content

IsLevelEnabled Method

Returns a value indicating whether the specified log level is enabled.

Definition​

Namespace: GHost.Core.Logging
Type: ILogger
Assembly: GHost.Core.dll

bool IsLevelEnabled(LogLevel level);

Parameters​

level LogLevel
The log level to test.

Return Value​

bool
true if the specified level is enabled; otherwise, false.

Remarks​

Use this method to check if a log level is enabled before performing expensive operations to generate log messages, such as string formatting or gathering contextual information.

The logger is designed to be as efficient as possible when a log is disabled, but a call to a logging method with a message that contains string formatting items will still incur the cost of computing and sending the arguments, even if the specified level is disabled. For many arguments this is not an issue, but if the arguments are expensive to compute, this can lead to unnecessary overhead when the log level is disabled.

By using IsLevelEnabled to check if the level is enabled before calling the logging method, you can avoid this overhead when the log level is disabled. For example:

LogLevel theLogLevel = GetLogLevelFromSomewhere();

if (logger.IsLevelEnabled(theLogLevel))
{
logger.Trace(
theLogLevel,
"UserDet",
"User {User} logged in",
GetUserFullDetails()); // Expensive method to get user details
}

This method is a general-purpose method for checking when the level is known only at runtime. if you know the level at compile time, it is more convenient to use one of the corresponding IsXxxxEnabled properties:

IsTraceEnabled
IsDebugEnabled
IsInfoEnabled
IsWarningEnabled
IsErrorEnabled
IsFatalEnabled