Skip to content

Configure

Matteo Gregoricchio edited this page Aug 14, 2024 · 10 revisions

A. Register services in the .NET DI Container

Note: please check Samples and Tests in the repository for additional examples on all configuration options.

// namespace Serilog.Ui.Web.Extensions
IServiceCollection AddSerilogUi(this IServiceCollection services, Action<ISerilogUiOptionsBuilder> optionsBuilder);

ISerilogUiOptionsBuilder offers a list of extension methods to register providers (check here for all the details).

Authorization

Authorization filters are retrieved from the .NET services container. By default, Serilog UI registers the filters as scoped services, to correctly access HttpContext from the IHttpContextAccessor interface.

Serilog.UI offers a list of default filters implementations:

/// <summary>
/// This filter runs an authentication check on a predefined authorization policy.
/// It's the same as per using the [Authorize(Policy="sample-policy")] attribute on a controller.
/// </summary>
ISerilogUiOptionsBuilder AddScopedPolicyAuthFilter(this ISerilogUiOptionsBuilder builder, string policy);

/// <summary>
/// This filter authorize local access without any additional check. It should be used only for testing and development reasons.
/// </summary>
ISerilogUiOptionsBuilder AddScopedAuthorizeLocalRequestsAuthFilter(this ISerilogUiOptionsBuilder builder);

/// Add <see cref="BasicAuthenticationFilter"/> as scoped service implementation of <see cref="IUiAuthorizationFilter"/>,
/// with a simple implementation that checks Basic Auth against a predefined user-password. <br />
/// It expects in the <see cref="IConfiguration"/> an object with this structure:
/// <code>
/// ...
/// "SerilogUi": {
///     "UserName": "user-sample",
///     "Password": "sample"
/// }, ...
ISerilogUiOptionsBuilder AddScopedBasicAuthFilter(this ISerilogUiOptionsBuilder builder);

/// <summary>
/// Add <see cref="BasicAuthenticationFilter"/> as scoped service implementation of <see cref="IUiAuthorizationFilter"/>,
/// registering a scoped custom implementation of <see cref="IBasicAuthenticationService"/>.
/// </summary>
ISerilogUiOptionsBuilder AddScopedBasicAuthFilter<T>(this ISerilogUiOptionsBuilder builder) where T : class, IBasicAuthenticationService;

To register custom filters, we suggest the user to use the extensions methods offered by the application.

The complete list can be read at src/Serilog.Ui.Core/Extensions/SerilogUiOptionsBuilderExtensions.cs

Additional Columns

Additional columns configuration is described in Providers page.

B. Register middleware in the .NET request pipeline

IApplicationBuilder UseSerilogUi(this IApplicationBuilder applicationBuilder, Action<UiOptions> options = null);

The UiOptions class offers the following configurations methods for the client app:

/// <summary>
/// Sets the route prefix to access log dashboard via browser.
/// The default value is <c>serilog-ui</c> and you can the dashboard by using <c>{base-path}/serilog-ui/</c>.
/// It MUST not end with a slash.
/// </summary>
UiOptions WithRoutePrefix(string routePrefix);

/// <summary>
/// Sets the type of the authentication the UI offers. You can choose between:
/// Custom [no authorization data offered]
/// Basic [user can authorize with a Basic Authorize header]
/// Jwt [user can authorize with a JWT Bearer Authorize header]
/// </summary>
UiOptions WithAuthenticationType(AuthenticationType authType);

/// <summary>
/// Run the authorization filters on the app routes, preventing an user from accessing completely the UI page if unauthorized.
/// </summary>
UiOptions EnableAuthorizationOnAppRoutes();

/// <summary>
/// Sets the URL for the home button.
/// </summary>
UiOptions WithHomeUrl(string homeUrl);

/// <summary>
/// Hide the serilog-ui brand in the top-right corner of the UI.
/// </summary>
UiOptions HideSerilogUiBrand();

/// <summary>
/// Sets the expand dropdowns by default property to true.
/// </summary>
UiOptions WithExpandedDropdownsByDefault()

/// <summary>
/// Injects additional CSS stylesheets into the index.html page.
/// Each call to the method adds a stylesheet entry.  
/// </summary>
/// <param name="path">A path to the stylesheet - i.e. the link "href" attribute</param>
/// <param name="media">The target media - i.e. the link "media" attribute</param>
InjectStylesheet(string path, string media = "screen");

/// <summary>
/// Injects additional Javascript files into the index.html page.
/// Each call to the method adds a stylesheet entry.
/// </summary>
/// <param name="path">A path to the javascript - i.e. the script "src" attribute</param>
/// <param name="injectInHead">
///   When true, injects the javascript in the <tag> instead of the <body> tag
/// </param>
/// <param name="type">The script type - i.e. the script "type" attribute</param>
UiOptions InjectJavascript(string path, bool injectInHead = false, string type = "text/javascript");

From Serilog.UI v3.0.1

/// <summary>
/// Sets the expand dropdowns by default property to true.
/// </summary>
UiOptions WithExpandedDropdownsByDefault()