Skip to content
Matteo Gregoricchio edited this page Aug 15, 2024 · 11 revisions

Reference namespace: Serilog.Ui.Core

API

IDataProvider interface

// the Provider unique name
string Name { get; }

// Fetch the data and returns a Tuple, consisting of
// - an IEnumerable of LogModel
// - the total number of matches obtained with the non-paginated query.
Task<(IEnumerable<LogModel> results, int total)> FetchDataAsync(
  FetchLogsQuery queryParams,
  CancellationToken cancellationToken = default);

Authentication

IUiAuthorizationFilter interface

/// <summary>
/// Authorizes a synchronous request.
/// </summary>
bool Authorize();

IUiAsyncAuthorizationFilter interface

/// <summary>
/// Authorizes an asynchronous request.
/// </summary>
Task<bool> AuthorizeAsync();

Note: DI Registration

To register any custom implementation in the .NET IServiceProvider, Serilog UI exposes a series of src/Serilog.Ui.Core/Extensions/SerilogUiOptionsBuilderExtensions.cs you can use.

Models

FetchLogsQuery class

// the unique provider name to run the query on
string DatabaseKey
// the 0-based page (used by the providers)
int Page
// the actual page (used by the client-app) [Linq.Skip]
int CurrentPage
// the amount of results to retrieve in a single request [Linq.Take]
int Count
// a log level filter
string Level
// a text search
string SearchCriteria
DateTime? StartDate
DateTime? EndDate
SearchOptions.SortProperty SortOn = SearchOptions.SortProperty.Timestamp;
SearchOptions.SortDirection SortBy = SearchOptions.SortDirection.Desc;

LogModel class

virtual int RowNo { private set; }
virtual string Level
virtual string Message
virtual DateTime Timestamp
virtual string Exception
virtual string Properties
virtual string PropertyType

// use to set the RowNo; 
// rowNoStart should be {FetchLogsQuery}.Count * {FetchLogsQuery}.Page
LogModel SetRowNo(int rowNoStart, int index)

Write a custom implementation

CTA!

A small heads up: if you write a custom provider or filter and you wish to share it with the OS community, feel free to open either a new issue or a new PR with the details of the implementation! We're always keen to see new contributions 🛩️

Implement a custom provider

Implement the IDataProvider interface.

Register the DataProvider as Scoped service implementation of IDataProvider in the .NET DI (IServiceProvider.AddScoped<IDataProvider, CustomImplementation();).

We suggest to register it inside the AddSerilogUi extension method, which offers IServiceProvider on ISerilogUiOptionsBuilder. This solution will let you use any helper available on ISerilogUiOptionsBuilder.

If you want to register a provider with additional columns (details can be found in the provider page), call the following method on ISerilogUiOptionsBuilder:

/// <param name="providerKey">The IDataProvider <see cref="IDataProvider.Name"/></param>
void RegisterColumnsInfo<T>(string providerKey) where T : LogModel;

If you want to disable sort by property (similar to what happens in ElasticSearch provider), call the following method on ISerilogUiOptionsBuilder:

/// <param name="providerKey">The IDataProvider <see cref="IDataProvider.Name"/></param>
void RegisterDisabledSortForProviderKey(string providerKey);

From Serilog.UI.Core v3.1.0

If you want to render in the UI the Exception column as a string (similar to what happens in SQL-based providers), call the following method on ISerilogUiOptionsBuilder:

/// <param name="providerKey">The IDataProvider <see cref="IDataProvider.Name"/>.</param>
void RegisterExceptionAsStringForProviderKey(string providerKey);

Implement a custom authorization filter

Implement IUiAuthorizationFilter or/and IUiAsyncAuthorizationFilter interfaces.

To provide them to the UI, either check these ext methods or manually register them in the .NET DI.