# Logging

## Overview

Smartstore uses Microsoft.Extensions.Logging with [Serilog](https://serilog.net/) for structured logging. Configuration lives in `appsettings.json` and can be overridden in `Config/usersettings.json`. Each log event is enriched with request data such as IP address, URL, customer ID, and user name. Events are written to rolling text files under `App_Data/Logs` and to the database table `Log` via a custom Serilog sink. Logs are viewable in the admin area under **System > Log**.

## Writing log entries

Inject `ILogger<T>` into any service or controller to write structured messages:

```csharp
public class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public void DoWork()
    {
        _logger.LogInformation("Processing started");
    }
}
```

Smartstore can also inject a logger via a public property. Add the property with a default [`NullLogger`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.abstractions.nulllogger) so it is always non-null and the container will replace it with a contextual logger at runtime:

```csharp
public ILogger Logger { get; set; } = NullLogger.Instance;
```

Controllers inheriting from `SmartController` already declare this `Logger` property, so you usually only add it to custom services that need optional logging without constructor injection.

## Request logging

The platform enables request logging middleware through `UseRequestLogging()`. It logs each HTTP request once with method, path, status code, elapsed time, and user context:

```csharp
public void Configure(IApplicationBuilder app)
{
    app.UseRequestLogging();
    // other middleware
}
```

## Activity log

`IActivityLogger` records high-level user actions such as logins, checkouts, or admin changes. Entries are stored in the `ActivityLog` table and surfaced in the admin UI:

```csharp
_activityLogger.LogActivity(KnownActivityLogTypes.EditSettings,
    "Settings updated for store {0}", store.Name);
```

## Notifications

Use `INotifier` to queue toast messages for the current request. They appear on the next page load:

```csharp
_notifier.Add(NotifyType.Success, "Order processed successfully");
```

Log cleanup Smartstore periodically purges old log entries from the database using the "Delete logs" scheduled task. By default, this task retains all entries with a level of **Error** or higher to ensure critical diagnostic data isn't lost.

If your application generates a high volume of errors, the `Log` table can grow significantly (e.g., hundreds of thousands of rows) and the cleanup task might become overwhelmed. To prevent this, you can opt-in to a more aggressive cleanup by using a hidden setting:

* Go to **Admin > Configuration > Settings > All Settings**.
* Edit the setting `CommonSettings.MinLogLevelToRetain`.
* Set its value to `Fatal`.

Once set, the automated cleanup task will also delete regular `Error` logs, keeping only `Fatal` errors indefinitely.


---

# 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://dev.smartstore.com/framework/platform/logging.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.
