# 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"
    }
  },
...
}
```
