Skip to content

Logging

Daniel edited this page Aug 16, 2020 · 13 revisions

CodeMonkeys.Logging

Nuget

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.

Getting started

Preparing the factory

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();
    }
}

Creating a log service

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.

Using the log service instance

Log methods

The service has methods for all the levels mentioned in the section Log levels. Just use the overload that suits you best.

Changing the behavior

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();

Options

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

Log levels

Trace

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

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

Info level is used for tracking the general flow of the application.

Sample:

LogService.Info($"Read configuration file at path: {path}");

Warning

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");

Error

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.

Critical

This level is used for failures that require immediate attention.

Clone this wiki locally