Logging

Introduction

To help you be aware of what’s happening with your application, Spark provides a logging service to allow you to log messages.

Under the hood, Spark uses the Serilog library.

Configuration

Logging can be configured in your .env file.

LOG_CHANNEL=file
LOG_LEVEL=information

Channels

Log channels control where your logs are written. There are currently 2 log channels..

  • file
  • console

Levels

Log levels control the minimum level of event that is logged to your channel.

For instance, if your log level is set to warning, only log messages with a warning level or higher will be logged. Debug and information logs would be ignored.

  • debug
  • information
  • warning
  • error
  • fatal

It is recommended to set your logs to warning in production to avoid noise in your logs.

Setup

The Spark logger is added to the DI container for you in AppServiceRegistration.AddAppServices().

services.AddLogger(config);

To use the logger, inject Spark.Library.Logging.ILogger via dependency injection.

@* blazor example *@
@page "/"
@inject ILogger _logger
// class example
private ILogger _logger;

public MyService(ILogger logger)
{
    this._logger = logger;
}

Writing Log Messages

Once injected, you may call any of these methods to log a message for the corresponding level.

    _logger.Debug(message);
    _logger.Information(message);
    _logger.Warning(message);
    _logger.Error(message);
    _logger.Fatal(message);

Stay up to date with Spark on Twitter or our Newsletter

Looking for .NET jobs? Check out Dotnet Jobs for weekly job postings.