# Logging

Good logs make it easier to identify and debug issues with your application at runtime.\
Ucommerce uses the built-in logging system from ASP.NET to make it simple for you to get and store log messages.

### Writing logs using ILogger\<TCategoryName>

Getting started with logging in Ucommerce is easy since it uses the built-in logging APIs.\
You can take a dependency on the `ILogger<TCategoryName>` interface and dependency injection will inject a logger into your class.

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

    public MyClass(ILogger<MyClass> logger) {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }
...
}
```

### Providers

The default Ucommerce templates don't make any changes from the default [logging provider setup](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-8.0#logging-providers).\
We recommend that you gather logs into a central log store to make discovery and debugging production issues easier.

Examples:

* [Seq](https://datalust.co/seq)
* [Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview)
* [Datadog](https://www.datadoghq.com/)

### EF Core queries and sensitive data

When debugging issues it can be very useful to see all the values given to, and the structure of, a query created by Entity Framework. Ucommerce makes it easy to set this up for the `UcommerceDBContext` by setting the `Ucommerce.Persistance.SensitiveDataLogging` option to true in your `appSettings.json` file.

```json
{
...
  "Ucommerce": {
    "Persistence":{
      "SensitiveDataLogging": true
    },
...
}
```

The option activates the following in the database context:

* Log queries to the console and debug output.
* [Enables sensitive data logging](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.enablesensitivedatalogging)
* [Enables detailed errors](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.enabledetailederrors)

If you are only interested in seeing the queries created you can set the logging level for the `Microsoft.EntityFrameworkCore.Database.Command` category to `Information` or lower.\
This should also work in production as opposed to the above option.

```json
{
...
  "Logging": {
    "LogLevel": {
      "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
  },
...
}
```


---

# 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.ucommerce.net/readme/miscellaneous/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.
