A simple Serilog log viewer for Serilog.Sinks.MSSqlServer, Serilog.Sinks.Postgresql, Serilog.Sinks.MongoDB and Serilog.Sinks.ElasticSearch sinks (other sinks will be added in the future).
Install the Serilog.UI NuGet package
Install-Package Serilog.UI
Then install one of the providers based upon your sink:
Serilog.UI.MsSqlServerProvider NuGet package:
Install-Package Serilog.UI.MsSqlServerProvider
Serilog.UI.PostgreSqlProvider NuGet package:
Install-Package Serilog.UI.PostgreSqlProvider
Serilog.UI.MongoDbProvider Nuget package:
Install-Package Serilog.UI.MongoDbProvider
Serilog.UI.ElasticSearchProvider Nuget package:
Install-Package Serilog.UI.ElasticSearchDbProvider
Then, add AddSerilogUi()
to IServiceCollection
in Startup.ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
// Register the serilog UI services
services.AddSerilogUi(options => options.UseSqlServer("ConnectionString", "LogTableName"));
// or
// services.AddSerilogUi(options => options.UseNpgSql("ConnectionString", "LogTableName"));
// or
// services.AddSerilogUi(options => options.UseMongoDb("ConnectionString", "DatabaseName", "CollectionName"))
// or
// services.AddSerilogUi(options => options.UseElasticSearchDb(endpoint: new System.Uri("http://localhost:9200"), indexName: "logging-index"))
}
In the Startup.Configure
method, enable the middleware for serving logs UI. Place a call to the UseSerilogUi
middleware after authentication and authorization middlewares otherwise authentication may not work for you:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
.
.
.
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// Enable middleware to serve log-ui (HTML, JS, CSS, etc.).
app.UseSerilogUi();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Default url to view log page is http://<your-app>/serilog-ui
. If you want to change this url path, just config route prefix:
app.UseSerilogUi(option => option.RoutePrefix = "logs");
Authorization configuration required
By default serilog-ui allows access to log page only for local requests. In order to give appropriate rights for production use, you need to configuring authorization. You can secure log page by allwoing specific users or roles to view logs:
public void ConfigureServices(IServiceCollection services)
{
services.AddSerilogUi(options => options
.EnableAuthorization(authOptions =>
{
authOption.AuthenticationType = AuthenticationType.Jwt; // or AuthenticationType.Cookie
authOptions.Usernames = new[] { "User1", "User2" };
authOptions.Roles = new[] { "AdminRole" };
})
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), "LogTableName"));
.
.
.
Only User1
and User2
or users with AdminRole
role can view logs. If you set AuthenticationType
to Jwt
, you can set jwt token and Authorization
header will be added to the request and for Cookie
just login into you website and no extra step is required.
- Additional columns are not supported and only main columns can be retrieved