ASP.NET MVC - Installing and working with Serilog - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Wednesday, July 8, 2026

ASP.NET MVC - Installing and working with Serilog

ASP.NET MVC - Installing and working with Serilog

Screenshot from the tutorial
Screenshot from the tutorial

ASP.NET MVC - Installing and Working with Serilog

In modern web applications, efficient logging is crucial for diagnosing issues and monitoring application performance. Serilog is a popular logging library for .NET that allows developers to log structured data. In this tutorial, we will explore how to install and configure Serilog in an ASP.NET MVC application.

What is Serilog?

Serilog is an open-source logging library for .NET that offers several advantages over traditional logging frameworks. It supports structured logging, which means logs can be enriched with additional context and easily queried. This structured approach is beneficial for debugging and analyzing logs in production environments.

Prerequisites

Before we begin, ensure you have the following:

  • Visual Studio installed (any recent version)
  • .NET SDK installed
  • Basic understanding of ASP.NET MVC

Step 1: Create a New ASP.NET MVC Project

  1. Open Visual Studio.
  2. Click on "Create a new project."
  3. Select "ASP.NET Web Application" and click "Next."
  4. Name your project (e.g., LoggingDemo) and choose a location.
  5. Select "MVC" in the templates and click "Create."

Step 2: Install Serilog

To install Serilog, we will use NuGet Package Manager. You can do this either through the Visual Studio UI or via the Package Manager Console.

Using NuGet Package Manager Console

  1. Open the Package Manager Console from Tools > NuGet Package Manager > Package Manager Console.

  2. Run the following command to install the Serilog package:

    Install-Package Serilog
    
  3. Additionally, install the sink packages for your preferred logging destination. For example, to log to the console, you can install:

    Install-Package Serilog.Sinks.Console
    

Using Visual Studio UI

  1. Right-click on your project in the Solution Explorer and select "Manage NuGet Packages."
  2. Search for "Serilog" and click "Install."
  3. Search for "Serilog.Sinks.Console" and install it.

Step 3: Configure Serilog

Now that we have installed Serilog, we need to configure it in our application. The best place to do this is in the Global.asax.cs file.

  1. Open Global.asax.cs.

  2. Add the following using directives at the top of the file:

    using Serilog;
    
  3. In the Application_Start method, configure Serilog:

    protected void Application_Start()
    {
        // Configure Serilog
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console() // Log to console
            .WriteTo.File("logs\\log-.txt", rollingInterval: RollingInterval.Day) // Log to file
            .CreateLogger();
    
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
    

This configuration sets the minimum logging level to Debug, logs to the console, and writes logs to a file that rolls over daily.

Step 4: Use Serilog in Your Controllers

To log messages from your controllers, you need to inject the logger. Here’s how to do it:

  1. Open any controller (e.g., HomeController.cs).

  2. Add a private logger field and log messages in your actions:

    using System.Web.Mvc;
    using Serilog;
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Log.Information("Accessed the Index page.");
            
            // Simulate an error
            try
            {
                throw new Exception("An error occurred!");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while accessing the Index page.");
            }
    
            return View();
        }
    }
    

Step 5: Run Your Application

  1. Build and run your application.
  2. Navigate to the Index page.
  3. Check the console output and the log file in the logs directory for the logged messages.

Conclusion

Integrating Serilog into your ASP.NET MVC application is a straightforward process that significantly enhances your logging capabilities. With structured logging, you can easily track and analyze your application's behavior, making it easier to troubleshoot issues in production.

Feel free to explore more Serilog features, such as logging to different sinks (e.g., databases, cloud services) and using enrichers to add contextual information to your logs. Happy logging!

Another screenshot from the tutorial
Another view from the tutorial

Connect with SkillBakery Studios

Explore more tutorials, tools, and resources:

Posted by SkillBakery Studios

No comments:

Post a Comment

Post Top Ad