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
<PackageReference Include="Serilog.Blazor.Abstractions" Version="1.0.7" />
<PackageVersion Include="Serilog.Blazor.Abstractions" Version="1.0.7" />
<PackageReference Include="Serilog.Blazor.Abstractions" />
paket add Serilog.Blazor.Abstractions --version 1.0.7
#r "nuget: Serilog.Blazor.Abstractions, 1.0.7"
#:package Serilog.Blazor.Abstractions@1.0.7
#addin nuget:?package=Serilog.Blazor.Abstractions&version=1.0.7
#tool nuget:?package=Serilog.Blazor.Abstractions&version=1.0.7
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.
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.
Expands to show exception detail and properties.
Source: SerilogGrid
Example: SampleApp
</details>
<details> <summary>SourceContextFilter</summary>
Zoom out to see total log entries by level and source context.
Source: SourceContextFilter
Example: SampleApp
</details>
<details> <summary>FilterBar</summary>
This is a reworking of the SourceContextFilter
component as a tabbed UI.
Source: FilterBar
Two Usage Options:
- Parameter-based (Flexible): Pass data as parameters for flexible usage
<FilterBar MetricsResults="@yourMetricsData"
LogLevels="@yourLogLevels"
SourceContextClicked="@OnSourceContextClicked" />
- 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
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)
- Install the SQL Server and RCL packages listed above. (Also the Serilog.Sinks.MSSqlServer package if you don't have it yet.)
- Implement abstract class LogLevels in your app. Example: ApplicationLogLevels
- 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.DefaultcolumnOptions
argument. This ensures theSourceContext
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();
If using the SearchBar, add an EF Core
IDbContextFactory<T>
to your startup. Example.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)
- Install the Postgres and RCL packages listed above.
- Implement abstract class LogLevels in your app.
- 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);
- 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.
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 | Versions 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. |
-
net9.0
- Coravel (>= 6.0.2)
- Microsoft.EntityFrameworkCore (>= 9.0.6)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.4)
- Serilog (>= 4.2.0)
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.