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.

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. We recommend that you gather logs into a central log store to make discovery and debugging production issues easier.

Examples:

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.

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

The option activates the following in the database context:

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.

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

Last updated

Was this helpful?