×

iFour Logo

What is Serilog in Asp.Net Core 3.1?

Kapil Panchal - October 22, 2020

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
What is Serilog in Asp.Net Core 3.1?

What is logging?


Logging is an essential needed feature for detecting and examine issues in applications. Logging frameworks make it easy to log event data to Configure in Advance for log targets. However, if your log file carries unstructured data, it becomesa nightmare to query the data.

Structured logging makes it easy to query event data by making sure that the data to be logged is written in that format which is a structured format. The format to be XML, JSON, or any other structured format that makes parsing the data easy. Structured logging also helps in processing the log files for log analytic.

Serilog in ASP.NET core 3.1


Serilog Launched in 2013, it is one of the newest logging frameworks. This unquestionably is not a bad thing, since the framework uses some of the latest and more advanced features of .NET. Structured logging apparently being the most noticeable, but also the concept of enrichment is what makes it unique compared to a lot of other logging frameworks.

It is constantly growing and new log targets are being launched every month. There is a large community around it. So, when you want a suggestion about it you find easily because so many peoples are using it.

Serilog is one type of third-party logging library that is used to plugs into the default ILogger of our application with its own implementations. With the help of Serilog, we can help developers to log the events into various destinations like console, file, database, and more. if you are already using a database in your ASP.NET Core Application, logging events to a database can be a very good option. Serilog also supports structured logging, which allows more details and information about the event to be logged. With structured logging in place, Serilog is also used to debug in a very logical way.

This is an alternative logging implementation that plugs into ASP.NET Core. It supports the same structured logging APIs, and receives log events from the ASP.NET Core framework class libraries, with adds a stack of features that make because of a more fascinatingalternative for a few sorts of apps and environments.

Why we use Serilog?


As many other libraries for .NET, of Serilog offers diagnostic logging to files, such as the console, and elsewhere. It is very easy to set up, has a clean API, Serilog is portable between recent .NET platforms.

Advantages of Serilog


  • Structured logging and enrichment
  • Great documentation and community
  • C# based configuration

Disadvantages of Serilog


Need to learn more features.

Stepwise example of Swagger


For implementing Serilog in ASP.Net Console, first, we will create a new project. For creating a new ASP.Net Core Web API, we will open Visual Studio Once Visual Studio is open, secondly, we will select the menu option File -> New -> Project.

Once the new project creation window pops up, we will select the ASP.Net Console Application and then we will tap on the Next button.

After creating a new Project you need to install some nugget packages for this project they are listed below notice when you install packages it is important to see the version of that packages.

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.EnvironmentVariables
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Dependencylnjection
  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console
  • Serilog.AspNetCore
  • Serilog.Sinks.File

After that replace the below code you can also change this code as per your requirement in project.

                  using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Serilog;

namespace Logger {


    class Program {

        static void Main (string [] args) {

      IConfiguration configuration = new ConfigurationBuilder ().AddEnvironmentVariables ().Build ();
      Log.Logger = new LoggerConfiguration ().WriteTo.File ("consoleapp.log").CreateLogger ();

      varserviceCollection = new ServiceCollection ();
      ConfigureServices (serviceCollection, configuration);

      varserviceProvider = serviceCollection.BuildServiceProvider ();

      serviceProvider.GetService(). SomeMethod();

      var logger = serviceProvider.GetService> ();

      logger.LogInformation ("Log in program.cs");

        }
        private static void ConfigureServices (IServiceCollection services, IConfiguration configuration)
    {
      services.AddLogging (configure =>configure.AddSerilog ()).AddTransient ();
            if (configuration["Log_Level"] == "true")
      {
        services.Configure (options =>options.MinLevel = LogLevel.Trace);

            } else {
        services.Configure (options =>options.MinLevel = LogLevel.Error);
            }
        }
    }
}
                

After that create a new class for Serilog and apply the name MyClass. You can apply another name also.

using System;
using Microsoft.Extensions.Logging;
class MyClass 
{
private readonlyILogger _logger;

public MyClass (ILogger logger) 
{
_logger = logger;
}
public void SomeMethod () 
{
_logger.LogInformation("Hello");
}

}

Looking to Hire Dedicated ASP.Net Core DeveloperĀ ? Contact Now

If the program has successfully run than you will get the below outputwhenever you run the program at that time myclass is called and generate the new log in this file.

 2020-09-10 09:55:57.718 +05:30 [INF] Hello
2020-09-10 09:55:57.729 +05:30 [INF] Log in progem.cs
2020-09-10 09:57:06.956 +05:30 [INF] Hello
2020-09-10 09:57:06.966 +05:30 [INF] Log in progem.cs
2020-09-10 10:40:46.175 +05:30 [INF] Hello
2020-09-10 10:40:46.185 +05:30 [INF] Log in program.cs
2020-09-10 10:41:28.220 +05:30 [INF] Log in program.cs
2020-09-10 10:42:27.071 +05:30 [ERR] Hello
2020-09-10 10:42:27.081 +05:30 [INF] Log in program.cs
2020-09-10 10:43:38.361 +05:30 [FTL] Hello
2020-09-10 10:43:38.371 +05:30 [INF] Log in program.cs
2020-09-10 10:44:30.360 +05:30 [WRN] Hello
2020-09-10 10:44:30.370 +05:30 [INF] Log in program.cs
2020-09-10 10:51:25.462 +05:30 [INF] Hello
2020-09-10 10:51:25.472 +05:30 [INF] Log in program.cs
                    

After downloading the project Unzip the file and open the solution in visual studio and run the Project .and test your code.

Conclusion


From this blog you will clearly able to understand what Serilog is and how to configure it using a given example.In short, Serilog isa type of third-party logging library that is used to plugs into the default ILogger of our application with its own implementation.Above we also have learned how to use Serilog with an Asp.NET Core application.We too have discussed how to create and execute a sample Serilog program in a project and its benefit.

What is Serilog in Asp.Net Core 3.1? What is logging? Logging is an essential needed feature for detecting and examine issues in applications. Logging frameworks make it easy to log event data to Configure in Advance for log targets. However, if your log file carries unstructured data, it becomesa nightmare to query the data. Structured logging makes it easy to query event data by making sure that the data to be logged is written in that format which is a structured format. The format to be XML, JSON, or any other structured format that makes parsing the data easy. Structured logging also helps in processing the log files for log analytic. Serilog in ASP.NET core 3.1 Serilog Launched in 2013, it is one of the newest logging frameworks. This unquestionably is not a bad thing, since the framework uses some of the latest and more advanced features of .NET. Structured logging apparently being the most noticeable, but also the concept of enrichment is what makes it unique compared to a lot of other logging frameworks. It is constantly growing and new log targets are being launched every month. There is a large community around it. So, when you want a suggestion about it you find easily because so many peoples are using it. Serilog is one type of third-party logging library that is used to plugs into the default ILogger of our application with its own implementations. With the help of Serilog, we can help developers to log the events into various destinations like console, file, database, and more. if you are already using a database in your ASP.NET Core Application, logging events to a database can be a very good option. Serilog also supports structured logging, which allows more details and information about the event to be logged. With structured logging in place, Serilog is also used to debug in a very logical way. This is an alternative logging implementation that plugs into ASP.NET Core. It supports the same structured logging APIs, and receives log events from the ASP.NET Core framework class libraries, with adds a stack of features that make because of a more fascinatingalternative for a few sorts of apps and environments. Read More: Unit Testing A Custom Middleware In Asp.net Core With Interface Why we use Serilog? As many other libraries for .NET, of Serilog offers diagnostic logging to files, such as the console, and elsewhere. It is very easy to set up, has a clean API, Serilog is portable between recent .NET platforms. Advantages of Serilog Structured logging and enrichment Great documentation and community C# based configuration Disadvantages of Serilog Need to learn more features. Stepwise example of Swagger For implementing Serilog in ASP.Net Console, first, we will create a new project. For creating a new ASP.Net Core Web API, we will open Visual Studio Once Visual Studio is open, secondly, we will select the menu option File -> New -> Project. Once the new project creation window pops up, we will select the ASP.Net Console Application and then we will tap on the Next button. After creating a new Project you need to install some nugget packages for this project they are listed below notice when you install packages it is important to see the version of that packages. Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.EnvironmentVariables Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration Microsoft.Extensions.Dependencylnjection Microsoft.Extensions.Logging Microsoft.Extensions.Logging.Console Serilog.AspNetCore Serilog.Sinks.File After that replace the below code you can also change this code as per your requirement in project. using System; using System.Collections.Generic; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Serilog; namespace Logger { class Program { static void Main (string [] args) { IConfiguration configuration = new ConfigurationBuilder ().AddEnvironmentVariables ().Build (); Log.Logger = new LoggerConfiguration ().WriteTo.File ("consoleapp.log").CreateLogger (); varserviceCollection = new ServiceCollection (); ConfigureServices (serviceCollection, configuration); varserviceProvider = serviceCollection.BuildServiceProvider (); serviceProvider.GetService(). SomeMethod(); var logger = serviceProvider.GetService> (); logger.LogInformation ("Log in program.cs"); } private static void ConfigureServices (IServiceCollection services, IConfiguration configuration) { services.AddLogging (configure =>configure.AddSerilog ()).AddTransient (); if (configuration["Log_Level"] == "true") { services.Configure (options =>options.MinLevel = LogLevel.Trace); } else { services.Configure (options =>options.MinLevel = LogLevel.Error); } } } } After that create a new class for Serilog and apply the name MyClass. You can apply another name also. using System; using Microsoft.Extensions.Logging; class MyClass { private readonlyILogger _logger; public MyClass (ILogger logger) { _logger = logger; } public void SomeMethod () { _logger.LogInformation("Hello"); } } Looking to Hire Dedicated ASP.Net Core DeveloperĀ ? Contact Now See here If the program has successfully run than you will get the below outputwhenever you run the program at that time myclass is called and generate the new log in this file. 2020-09-10 09:55:57.718 +05:30 [INF] Hello 2020-09-10 09:55:57.729 +05:30 [INF] Log in progem.cs 2020-09-10 09:57:06.956 +05:30 [INF] Hello 2020-09-10 09:57:06.966 +05:30 [INF] Log in progem.cs 2020-09-10 10:40:46.175 +05:30 [INF] Hello 2020-09-10 10:40:46.185 +05:30 [INF] Log in program.cs 2020-09-10 10:41:28.220 +05:30 [INF] Log in program.cs 2020-09-10 10:42:27.071 +05:30 [ERR] Hello 2020-09-10 10:42:27.081 +05:30 [INF] Log in program.cs 2020-09-10 10:43:38.361 +05:30 [FTL] Hello 2020-09-10 10:43:38.371 +05:30 [INF] Log in program.cs 2020-09-10 10:44:30.360 +05:30 [WRN] Hello 2020-09-10 10:44:30.370 +05:30 [INF] Log in program.cs 2020-09-10 10:51:25.462 +05:30 [INF] Hello 2020-09-10 10:51:25.472 +05:30 [INF] Log in program.cs After downloading the project Unzip the file and open the solution in visual studio and run the Project .and test your code. Conclusion From this blog you will clearly able to understand what Serilog is and how to configure it using a given example.In short, Serilog isa type of third-party logging library that is used to plugs into the default ILogger of our application with its own implementation.Above we also have learned how to use Serilog with an Asp.NET Core application.We too have discussed how to create and execute a sample Serilog program in a project and its benefit.
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Technical Content Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail
Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 

Blog Our insights

Quarkus vs Spring Boot - What’s Ideal for Modern App Development?
Quarkus vs Spring Boot - What’s Ideal for Modern App Development?

Spring Boot has long been a popular choice for developing custom Java applications. This is owing to its comprehensive features, impeccable security, and established ecosystem. Since...

Power BI Forecasting Challenges and Solutions
Power BI Forecasting Challenges and Solutions

Microsoft Power BI stands out for detailed data forecasting. By inspecting data patterns and using statistical models, Power BI provides a visual forecast of things to anticipate in...

Kotlin vs Java - Top 9 Differences CTOs Should Know
Kotlin vs Java - Top 9 Differences CTOs Should Know

Choosing the right programming language becomes crucial as it greatly influences the success of a software development project. When it comes to selecting the best programming language...