ILoggerService

Overview

The ILoggerService service defines a contract for logging functionalities, supporting logging of messages with a specified trace level and optional formatting arguments.

Wisej.AI employs the ILoggerService to log a variety of events and information. The default implementation directly utilizes the System.Diagnostics.Trace class. However, you have the flexibility to implement your own logging service, allowing you to integrate any logging library of your choice.

To implement logging in Wisej.AI, you need to work with two primary methods: Log(TraceLevel level, string message, params object[] arguments) and Log(TraceLevel level, Exception exception).

The Log(TraceLevel level, string message, params object[] arguments) method enables you to log messages that can include .NET formatting placeholders, along with any number of replacement values. This is particularly useful for creating dynamic and informative log entries.

On the other hand, the Log(TraceLevel level, Exception exception) method is specifically designed to log exceptions. This method allows you to capture and record exception details.

Utilize the TraceLevel to decide whether, how, and where a message should be logged. The default implementation serves as a straightforward bridge to the Trace class:

public void Log(TraceLevel level, string message, params object[] arguments)
{
    if (String.IsNullOrEmpty(message))
        return;

    switch (level)
    {
        case TraceLevel.Off:
            break;

        case TraceLevel.Info:
            Trace.TraceInformation(message, arguments);
            break;

        case TraceLevel.Error:
            Trace.TraceError(message, arguments);
            break;

        case TraceLevel.Warning:
            Trace.TraceWarning(message, arguments);
            break;

        case TraceLevel.Verbose:
            Trace.WriteLine(String.Format(message, arguments));
            break;

        default:
            Trace.WriteLine(String.Format(message, arguments));
            break;
    }
}

As an alternative to registering your own ILoggerService, you can register a TraceListener with the System.Diagnostics.Trace.Listeners collection. For instance, you can use NLogTraceListener as an example to direct log output to NLog, enabling advanced logging features and configurations without the need to implement a custom logging service.

Default Implementation

In Wisej.AI, the default logging mechanism is implemented within the DefaultLoggerService. This service utilizes the .NET Trace class to record and log all messages generated by Wisej.AI components.

You have the flexibility to either implement your own version of ILoggerService or enhance logging functionality by adding custom trace listeners to Trace.Listeners. By creating a custom implementation of ILoggerService, you can tailor the logging process to suit specific requirements or use different logging frameworks. Alternatively, by adding custom trace listeners, you can extend the default logging behavior to capture log entries in various formats or direct them to specific outputs, such as files, event logs, or external monitoring systems.

Last updated