Usah mimpi..

Usah mimpi menjadi camar kalau tidak pernah bercarah. “Usah mimpi..” is published by Ahmad Zulfadhli K..

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Implementing Logging in a .NET Core Web Application using Serilog

Create a new MVC application from Visual Studio. By default, Logging is enabled in the application via the ILogger interface. It has got some built-in providers for writing the log information to the console, event log as well as for third-party providers such as NLog, Serilog, etc.

For example, if you want to write logging information to the console window or event log, you will need to configure it in the CreateDefaultHostBuildermethod in the Program.cs file as shown below.

In the case of a web app, you will get an ILoggerfrom the DI container and use that object for writing into the configured log providers. Let's see how we can do that in the HomeController

First, create a private variable for the logger interface.

Then modify the constructor as shown below

Now, use the LogInformation method available in the logger interface to write a message into the log

If we run the application and go to the home page now, you will see this message written to the console.

To write an error, we normally use LogError method as shown below

And the output will be

First, you will need to install the necessary packages given below.

Modify the appsettings.json file to include the path for the log file

Then modify the Program.cs file to read these values from the JSON file

Now, to hook up Serilog provider, import the namespace first and then modify CreateHostBuilder method to set up the logger

Here’s the method is full

Now if we run the application, you will see the information being written to the file mentioned in the path.

Add a comment

Related posts:

Why you should contribute to Open Source

My first step with FOSS was during the summer of 2014, where I got introduced to DGPLUG(Durgapur Linux Users Group). Every year, they conduct a training over IRC covering a wide range of topics…

Two

Two days before our first date, I didn’t know what to expect. “Two” is published by Kingdom Orjiewuru.

Awesome Roofing Company!

I recently had the pleasure of working with ASAP Roofing Pros & Exteriors and I couldn’t be happier with the results. From the initial consultation to the final walkthrough, the team was…