# Logging & Error Handling

## Logging

All logging within Wisej.AI is managed via the [ILoggerService](https://docs.wisej.com/ai/components/api/services/iloggerservice). The default implementation is provided by [DefaultLoggerService](https://docs.wisej.com/ai/components/api/services/iloggerservice/wisej.ai.services.defaultloggerservice), which forwards the log entries to [System.Diagnostics.Trace](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace) according to the specified `TraceLevel`.

You can set up your own logging implementation by registering a service that implements the `ILoggerService` interface. The optimal location for this registration is within the static constructor of the `Program` class.

{% tabs %}
{% tab title="C#" %}

```csharp
static class Program
{
    static Program()
    {
        Application.Services.
            .AddOrReplaceService<ILoggerService, MyLoggerService>();
    }
}
```

{% endtab %}

{% tab title="VB.NET" %}

```vbnet
Module Program
    Shared Sub New()
        Application.Services.AddOrReplaceService( _
            Of ILoggerService, MyLoggerService)()
    End Sub
End Module
```

{% endtab %}
{% endtabs %}

## Error Handling

When using an AI provider, several types of errors can occur: channel errors, provider errors, server errors, and model errors.

1. **Channel Errors**: These involve issues with communication, such as loss of connectivity or network problems.
2. **Provider Errors**: These are related to your account with the AI provider, such as exceeding credit or usage limits.
3. **Server Errors**: Related to issues on the server side which might affect availability or performance.
4. **Model Errors**: These generally pertain to payload construction, token limits, or other constraints specific to the AI model being used.

If an error occurs while using the AI directly through a `SmartPrompt.AskAsync` call, you can handle it by wrapping the call in a try/catch block. This allows you to manage exceptions effectively as your code processes the response.

If an error occurs while using the AI through a `SmartAdapter`, and the `SmartAdapter` is connected to a `SmartHub` (as it should be), you can manage the error using the [SmartHub.Error](https://docs.wisej.com/ai/components/api/smarthub#error) event. However, if the adapter is not connected to a hub and you are using the `RunAsync()` method (or its variations) directly, you can manage errors by wrapping the call in a try/catch block.

If an error occurs while using a `SmartSession` instance, you can handle it by utilizing the `SmartSession.Error` event to manage the error.

The error event allows you to modify the last AI response and the assistant's response, effectively allowing you to "simulate" an AI response. To achieve this, you can handle the error (or override the `OnError` method) by adjusting the `ReplacementMessage` or changing the `Text` property of the predefined `ReplacementMessage` (which defaults to the exception message). Then, set the `Handled` property to `true`. This indicates that the error has been managed and that your custom response should be used.

{% tabs %}
{% tab title="C#" %}

```csharp
this.hub1.Error += (s, e) =>
{
    if (IsNetworkError(e.Exception)
    {
        e.ReplacementMessage.Text  = 
            "We're currently experiencing a temporary communication issue. " +
            "Please try again later or reach out to support at...";
            
        e.Handled = true;
    }
}
```

{% endtab %}

{% tab title="VB.NET" %}

```vbnet
AddHandler Me.hub1.Error, Sub(s, e)
    If IsNetworkError(e.Exception) Then
        e.ReplacementMessage.Text = 
          "We're currently experiencing a temporary communication issue. " & _
          "Please try again later or reach out to support at..."
        e.Handled = True
    End If
End Sub
```

{% endtab %}
{% endtabs %}


---

# 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/concepts/logging-and-error-handling.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.
