# ILoggerService

## Overview

The [ILoggerService](/ai/components/api/services/iloggerservice.md) 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:

```csharp
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` ](https://github.com/NLog/NLog/wiki/NLog-Trace-Listener-for-System-Diagnostics-Trace)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`](/ai/components/api/services/iloggerservice/wisej.ai.services.defaultloggerservice.md). This service utilizes the .NET [`Trace`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace) class to record and log all messages generated by Wisej.AI components.&#x20;

You have the flexibility to either implement your own version of `ILoggerService` or enhance logging functionality by adding custom trace listeners to [`Trace.Listeners`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/built-in-services/iloggerservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
