-
Notifications
You must be signed in to change notification settings - Fork 0
Logging
The CodeMonkeys.Logging
package provides a variety of different logging features and components. It is heavily inspired by the ASP.Core logging mechanisms.
For advanced use cases it is recommend to use a dedicated 3rd party logging framework.
The LogServiceFactory
is the central class when it's about creating and managing the log service creation. Here you can create service instances and also register new log targets.
But let's see a sample:
class Program
{
static void Main(string[] args)
{
// Adds file logging with default configuration
LogServiceFactory.Instance.AddFile();
// Adds debug logging with default configuration
LogServiceFactory.Instance.AddDebug();
// Adds console logging with default configuration
LogServiceFactory.Instance.AddConsole();
}
}
class Program
{
static void Main(string[] args)
{
...
// Creates a logger which will log to file, debug and console
var logger = LogServiceFactory.Instance.Create(nameof(Program));
}
}
Important: When you create a logger via the factory the implementation of it is always a composition from the previously registered services.
The service has methods for all the levels mentioned in the section Log levels. Just use the overload that suits you best.
The behavior of the service can be changed over a static property. You can find more about it in the section Options.
To disable the entire composition of a log service instance you can just set the IsEnabled
property to false.
To only disable a specific service contained in the composition you can do the following:
// disable
logger.DisableScopedService<DebugLogService>();
// or
logger.DisableDebugLogging();
// enable
logger.EnableScopedService<DebugLogService>();
// or
logger.EnableDebugLogging();
The ScopedLogService<TOptions>
implementations have a static Options
property with which you can control the service behavior. Please refer to the corresponding wiki page to see the implementation specific options.
The abstract LogOptions
base class has the following properties:
Name | Type | Description | Default value |
---|---|---|---|
MinLevel | LogLevel | All log messages below this level are ignored. | LogLevel.Info |
TimestampFormat | string | The timestamp format used for the log messages. | yyyy-MM-dd HH:mm:ss |
Trace level is used for information that's valuable only to a developer debugging an issue. Because these messages may contain sensitive application data we recommend to to disable this level in production.
Sample:
SecretApi: { Name: "example", key: "exampleKey" }
Debug level is used for information that has short-term usefulness during development and debugging. We recommend to disable this level in production due to high volume of logs.
Sample:
LogService.Debug($"ENTER {method}");
...
LogService.Debug($"LEAVE {method}");
Info level is used for tracking the general flow of the application.
Sample:
LogService.Info($"Read configuration file at path: {path}");
The warning level is used for abnormal or unexpected events in the application flow. This can include errors or other conditions that don't cause the application to stop, but which may need to be investigated. Handled exceptions are a common place to use this level.
Sample:
LogService.Warning($"FileNotFoundException for file example.txt");
The error level is used for errors and exceptions that cannot be handled. These messages indicate a failure in the current activity, not an application-wide failure.
This level is used for failures that require immediate attention.