Laravel like log viewer for SeriLog (Asp.NET MVC)

Muthukumar Thevar
4 min readNov 26, 2020

--

SeriLog Logviewer

Well, there are a lot of options out there to view the generated logs by the SeriLog. Most(90%) of them are cloud and paid services.

Introduction to SeriLog:

SeriLog is a robust and versatile logging library for .NET applications. It is known for its simplicity, performance, and extensibility. Unlike other logging libraries, SeriLog is structured logging friendly which means it can retain the structure of log events even when the events are serialized into different formats or sent to different output destinations. This feature enables richer log querying and analysis, making it a preferred choice for developers.

Professionally being a .net developer, I too work on many other languages like PHP, NodeJs, React, etc.

In one of my projects, I had to use the laravel framework which was very new for me. I read some documentation and started off my setup and coding with the laravel framework.

I had to configure logs in the application so I stumbled upon this log viewer https://github.com/rap2hpoutre/laravel-log-viewer which was very easy to configure and I found it very reliable and handy.

I loved the UI and the way it shows the logs and all which inspired me to make this similar log viewer for asp.net MVC.

So I head on to copy the source code

Asp.net mvc
Step 1 - Create New ASP.NET Web Application
asp.net mvc controller for serilog
Step 2 - Add new LogsController
serilog log viewer in asp.net mvc
Step 3 - Add new View

Now time to install the required packages.

Install-Package SeriLogInstall-Package Serilog.Sinks.File
serilog nuget package
Step 4 - Install package SeriLog & SeriLog.Sinks.File
serilog configuration in asp.net mvc
Let’s configure the serilog first in order to read and parse the logs in an efficient way.
//Step 5 - Configure the serilog
public static ILogger GetLogger<T>() => new LoggerConfiguration().WriteTo.File(new JsonFormatter(),AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/SeriLogs.txt", rollingInterval: RollingInterval.Day,fileSizeLimitBytes: 5242880,rollOnFileSizeLimit: true, shared: true).CreateLogger().ForContext<T>();

You can use a common class file or a base class to get the logger.

logging in asp.net mvc
This is how it can be used if you are using a common class.

If you are using a dependency injection library like Autofac then use the below line to register the ILogger

builder.Register<ILogger>((c, p) =>
{
return new LoggerConfiguration().WriteTo.File(new JsonFormatter(),AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "/SeriLogs.txt", rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 5242880, rollOnFileSizeLimit: true, shared: true).CreateLogger();
}).SingleInstance()

You can read about various configuration options over here: https://github.com/serilog/serilog/wiki/Configuration-Basics

Now let's begin with a small example

SeriLog Example
SeriLog Example C#
Logging Example

The above code will log an error when we will call the “SomeTask” action method.

After calling the method it threw an expected error.

asp.net mvc exception handling

Now let's see the error log generated by our SeriLog.

Remember while configuring the logger we had specified the App_Data directory for the log files to be generated.

{
"Timestamp": "2020-11-12T17:35:50.9574706+05:30",
"Level": "Error",
"MessageTemplate": "Error occured in SomeTask",
"Exception": "System.DivideByZeroException: Attempted to divide by zero.\r\n at SerilogViewer.Controllers.HomeController.SomeTask() in C:\\Users\\MuthukumarThevar\\source\\repos\\SerilogViewer\\SerilogViewer\\Controllers\\HomeController.cs:line 42",
"Properties": {
"SourceContext": "SerilogViewer.Controllers.HomeController"
}
}
serilog json format

This is the default output of the serilog viewer in JSON format which is very sweet and simple however, you can take a look for various formatting option over here: https://github.com/serilog/serilog/wiki/Formatting-Output

Final Step

Copy the PHP source from this view https://github.com/rap2hpoutre/laravel-log-viewer/blob/master/src/views/log.blade.php and make changes to support the razor view.

This is how I have converted to razor syntax except for the download & delete links. Copy the full code (https://gist.github.com/mak-thevar/16092506441f7dc95f0e65343183990a)to the view created in Step 3.

Full Code is Available Here: https://gist.github.com/mak-thevar/16092506441f7dc95f0e65343183990a
Exception handling asp.net mvc using serilog
Final Output

Conclusion:

Through this guide, we have successfully set up a Laravel-inspired log viewer for ASP.NET MVC using SeriLog. This log viewer not only enhances the visibility into the application’s behavior but also provides a structured and user-friendly way to analyze log events. By leveraging the power of SeriLog and the aesthetic appeal of the Laravel log viewer, we’ve created a useful tool that can significantly aid in monitoring and debugging ASP.NET MVC applications.

I invite you to share your experiences, insights, or any challenges you faced while setting up this log viewer. Your feedback is invaluable, and I’m here to answer any queries you may have. If you’ve had a different approach to setting up a log viewer in ASP.NET MVC, I’d love to hear about it. Feel free to drop your comments below or reach out to me through my contact information provided in my profile. Together, we can explore and learn more about enhancing logging capabilities in ASP.NET MVC.

GitHub Source Code : https://github.com/mak-thevar/DotNetSeriLogViewer
My LinkedIn Profile : https://linkedin.in/mak11

--

--

Muthukumar Thevar
Muthukumar Thevar

Written by Muthukumar Thevar

Passionate Programmer | Fitness Enthusiast | Curious Mind | Love Exploring The Universe | Humanist

No responses yet