Serilog.Blazor.Abstractions 1.0.7

dotnet add package Serilog.Blazor.Abstractions --version 1.0.7
                    
NuGet\Install-Package Serilog.Blazor.Abstractions -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Blazor.Abstractions" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Blazor.Abstractions" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="Serilog.Blazor.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Serilog.Blazor.Abstractions --version 1.0.7
                    
#r "nuget: Serilog.Blazor.Abstractions, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Serilog.Blazor.Abstractions@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Serilog.Blazor.Abstractions&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=Serilog.Blazor.Abstractions&version=1.0.7
                    
Install as a Cake Tool

This is a collection of Blazor components to improve your Serilog experience, currently targeting SQL Server and Blazor Server. NuGet packages:

Components

<details> <summary>LevelToggle</summary>

Use this to change log levels at runtime in your app for whatever namespaces/source contexts you define. This is handy when you need to temporarily increase log detail in a specific area to troubleshoot a production issue.

image

You define your own levels of course. Screenshot above is just a sample.

Source: LevelToggle

Example: SampleApp

</details>

<details> <summary>SerilogGrid</summary>

Scrolling grid of log entries.

image

Expands to show exception detail and properties.

image

Source: SerilogGrid

Example: SampleApp

</details>

<details> <summary>SourceContextFilter</summary>

Zoom out to see total log entries by level and source context.

image

Source: SourceContextFilter

Example: SampleApp

</details>

<details> <summary>FilterBar</summary>

This is a reworking of the SourceContextFilter component as a tabbed UI.

image

Source: FilterBar

Two Usage Options:

  1. Parameter-based (Flexible): Pass data as parameters for flexible usage
<FilterBar MetricsResults="@yourMetricsData" 
           LogLevels="@yourLogLevels" 
           SourceContextClicked="@OnSourceContextClicked" />
  1. Injection-based (Original): Uses dependency injection (backward compatible)
<FilterBarQuery SourceContextClicked="@OnSourceContextClicked" />

Example: SampleApp

</details>

<details> <summary>SearchBar</summary>

Search your logs with a variety of shortcuts. Save searches for easy reuse. Supported syntax:

  • enclose text in square brackets to search the source context field.
  • use a pound sign prefix to search the request Id property.
  • use the @ sign prefix to search the log level, e.g. @warn or @err or @info
  • use a minus sign prefix followed by number and duration unit, e.g. -15m for within 15 minutes or -1d for one day ago

image

Source: SearchBar

Example: SampleApp

This has a saved search feature that requires an EF Core DbContext implementing this interface ISerilogSavedSearches.

After implementing this interface on your DbContext, add a migration to add the underlying table to your database.

</details>

Getting Started (SQL Server)

  1. Install the SQL Server and RCL packages listed above. (Also the Serilog.Sinks.MSSqlServer package if you don't have it yet.)
  2. Implement abstract class LogLevels in your app. Example: ApplicationLogLevels
  3. In your app startup, create your ApplicationLogLevels instance (or whatever you decide to call it), and use it as the basis of your Serilog configuration. Also be sure to include the SqlServerColumnOptions.Default columnOptions argument. This ensures the SourceContext is captured as a dedicated column in your logs. Example:
var logLevels = new ApplicationLogLevels();

Log.Logger = logLevels
  .GetConfiguration()  
  .WriteTo.MSSqlServer({your connection string}, new MSSqlServerSinkOptions()
  {
    AutoCreateSqlTable = true,
    TableName = "Serilog", // whatever table name you like
    SchemaName = "log", // whatever schema you like
  }, columnOptions: SqlServerColumnOptions.Default) // this is important
  .Enrich.FromLogContext()
  .CreateLogger();
  1. If using the SearchBar, add an EF Core IDbContextFactory<T> to your startup. Example.

  2. Call extension method AddSerilogUtilities in your app startup. Example:

// to enable search bar saved searches. You might already have a db context being added somewhere, but it needs to be a factory specifically for this library. Lifetime doesn't matter. I use singleton here, but it can be scoped
builder.Services.AddDbContextFactory<ApplicationDbContext>(config => config.UseSqlServer({your connection string}), ServiceLifetime.Singleton);

// adds log level toggle and infrastructure for querying Serilog table. Use your serilog table name and schema. Also, chang ethe TimestampType according to how log entries are timestamped. I'm using Local in the example here
builder.Services.AddSerilogUtilities({your connection string}, logLevels, "log", "Serilog", TimestampType.Local);

Getting Started (Postgres)

  1. Install the Postgres and RCL packages listed above.
  2. Implement abstract class LogLevels in your app.
  3. In your app startup, initialize Serilog with user your LogLevels class (whatever you call it) and call a couple of the provided extension methods. Note the use of custom column options PostgresColumnOptions.
var logLevels = new ApplicationLogLevels();

Log.Logger = logLevels
  .GetConfiguration()
  .WriteTo.Console() // optional, but I use this
  .WriteTo.PostgreSQL({your connection string}, "serilog", columnOptions: PostgresColumnOptions.Default, needAutoCreateTable: true)	
  .Enrich.FromLogContext()	
  .CreateLogger();

builder.Services.AddSerilog();
builder.Services.AddSerilogUtilities({your connection string}, logLevels, "public", "serilog", TimestampType.Utc);
  1. After initial startup for the first time, run this SQL script on your Postgres database to add the id column. It's not created by default when you use custom column options.
ALTER TABLE serilog ADD id serial PRIMARY KEY

Now your ready to build your own Serilog viewer page. Refer to sample above for ideas.

Goodies and Extensions

BeginRequestId

Correlating log entries in API or non-SPA web apps can be done with ASP.NET Core's HttpContext.TraceIdentifier property. This is not helpful in Blazor due to how it works with HttpContext. As an alternative, this library provides a generic log correlation extension method BeginRequestId. Use this at the beginning of a method where you want to ensure that all logs written in that scope have a common identifier. Example. This works with LoggingRequestIdProvider to provide an auto-incrementing value when it's called.

@inject LoggingRequestIdProvider RequestId

private void LogThis()
{
  // attach an id to all logging in this method
  Logger.BeginRequestId(RequestId);

  Logger.LogInformation("This is an info log message");

  Logger.LogDebug("This is a debug message");

  // logs from this method call will be correlated with requestId in scope here
  SampleService.DoWork();
}

When logs are correlated, you can click the Request Id in the grid view and find all the logs related to that request, spread across any services in scope at the time.

image

SerilogCleanup

Implement retention periods for various log levels with the AddSerilogCleanup method, used at startup:

builder.Services.AddSerilogCleanup(new() 
{ 
  ConnectionString = {your connection string}, 
  TableName = "log.Serilog", // your serilog table name
  Debug = 5, // retention period in days
  Information = 20,
  Warning = 20,
  Error = 20
});

Then in your pipeline, call RunSerilogCleanup with a desired interval. This uses Coravel Scheduling, so use its syntax for setting the interval.

var app = builder.Build();

app.Services.RunSerilogCleanup(interval => interval.DailyAt(0, 0)); 

Motivation

When self-hosting Serilog, there's no built-in log view experience. You have to query the serilog table manually and/or build your own UI or query feature. There are products like Seq and Graylog that provide a very polished log search and view exeperience, but have costs of their own. The reason for self-hosting to begin with is to avoid those costs. Furthermore, traditional homegrown log viewers have not provided the kind of insights and capabilities I'm looking for when examining logs. The goal for this project is to build the most capable log view experience I can, addressing longstanding pain points I've come across -- then offer it as a Razor Class Library NuGet package.

With .NET Aspire coming online recently, it has potential to disrupt and improve logging in ASP.NET Core due to its integrated open telemetry support and viewer dashboards. So, I'm very late to this party, and this project may be irrelevant in the short term. But I'm not convinced yet that Aspire's logging/otel does everything I want it to. Furthermore, many apps implement Serilog already, and I think there's a case for meeting apps where they are rather than pushing them to implement Aspire. (I want Aspire to succeed, and am happy to keep tabs on it as it evolves.)

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Serilog.Blazor.Abstractions:

Package Downloads
Serilog.Blazor.RCL

UI components for Serilog: grid view, log level selector, saved searches, and metrics

Serilog.Blazor.SqlServer

Serilog Blazor components for SQL Server

Serilog.Blazor.ApiConnectorClient

Query Serilog data from apps implementing Serilog.Blazor.ApiConnector

Serilog.Blazor.Postgres

Serilog Blazor components for PostgreSQL

Serilog.Blazor.ApiConnector

Securely expose Serilog data with API endpoints

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 194 7/9/2025
1.0.6 103 7/5/2025
1.0.4 131 7/4/2025
1.0.3 153 7/4/2025