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.

Spark uses the Serilog library to accomplish this. It is setup out of the box to use a console and file logger.

Configuration

Logging is configured in your appsettings.json file.

...
"Serilog": {
    "MinimumLevel": {
        "Default": "Information",
        "Override": {
            "Microsoft": "Warning",
            "System": "Warning"
        }
    },
    "Using": [
        "Serilog.Sinks.Console",
        "Serilog.Sinks.File"
    ],
    "WriteTo": [
        {
            "Name": "Console"
        },
        {
            "Name": "File",
            "Args": {
                "path": "Storage/Logging/Spark.log",
                "rollingInterval": "Day"
            }
        }
    ]
},

Channels

Log channels control where your logs are written. There are 2 log channels setup for you out of the box.

  • file
  • console

Setup

The Serilog logger is configured for you in Program.cs.

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration).CreateLogger();

builder.Host.UseSerilog();

Using the Logger

To use the logger, use the static Log class in your razor components or classes.

@page "/"
@code {
    protected override void OnInitialized()
    {
        Log.Information("This is the homepage.");
    }
}

Writing Log Messages

Any of these methods are available to log a message for the corresponding level.

    Log.Verbose(message);
    Log.Debug(message);
    Log.Information(message);
    Log.Warning(message);
    Log.Error(message);
    Log.Fatal(message);