Integrate logging in a ASP.NET Core application using Serilog and Seq
November 20, 2022 by Anuraj
DotNetCore AspNetCore Logging Serilog Seq
This post is about how to integrate logging in a asp.net core application using Serilog and Seq. Serilog is a logging framework for .NET. Seq is the intelligent search, analysis, and alerting server built specifically for modern structured log data. We can use Seq for free if you’re developing solo and requires a license if you’re building professionally. For more details visit seq pricing page
First we need to create a .NET Core application, I am using an MVC application in this demo. I am creating the application using dotnet new mvc -o SeqSerilogDemoMvc
command. Now run the application, I want to compare the logging before enabling serilog and after that. We will be getting something like this.
Next I am adding the nuget packages to support logging with serilog. I need to add Serilog.AspNetCore
using the command dotnet add package Serilog.AspNetCore --version 6.0.1
- for this demo I am using a stable version. Then we can modify the program.cs
file to log using Serilog like this.
Now run the application again and we can see it is logging lot of more information.
We can see the difference the log output. Next we can configure Seq with Docker and configure the application to push the log to seq instead of console. We can run the docker seq container with this command - docker run --name seqlogger -d -p 5341:5341 -p 9000:80 -e ACCEPT_EULA=Y datalust/seq
. This command will run seq logging service in port 5341 and the seq admin UI is port 9000. Once it started running, we can browse the http://localhost:9000 and check the seq service is running or not. To support seq logging, serilog provides a sink. We need to add the reference of that nuget package. We can do it with this command dotnet add package Serilog.Sinks.Seq --version 5.2.2
. Here also I am using the stable version. Now we need modify our code to support logging to seq like this.
If note, only one line of code added - .WriteTo.Seq("http://localhost:5341")
the URL is our docker container instance. Now run the application and we can see the logs in console and the seq UI at http://localhost:9000. Here is the screenshot of the seq UI.
There is a dashboard view available as well for seq. We can click on the dashboard link and access it.
Most of the times, we want to configure logging from configuration instead of code. So we can do something like this in the config so that it can log to development and production environments.
To do this we can modify the appsettings.json
like this.
And modify our code like this.
Now it will work based on configuration - we can deploy it and switch to different endpoint and different providers. We can improve the logs with logging the http request object and we can add logging extra parameters to the logs as well. We can do this like this.
This way you will be able to use Serilog and Seq for improving logging and monitoring ASP.NET Core applications. We can use the Seq UI to query using events using SQL like language.
Happy Programming.
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub