Create your own file logger

.NET Core comes with a nice feature for logging.

With this example, we have extended .NET Core's logging feature so you can log to text files.

C# coding challenges

C# coding challenges

Our online code editor allows you to compile the answer.

The example consists of an .NET 5 ASP.NET Core Web API, which is integrated with a database through Entity Framework.

In-addition, the Web API has a hosted service running in the background. This job runs every 60 seconds.

Crucially, this job has had logging added to it. This is so we know that the job is running successfully.

Software

This is the software that will need to be installed onto your machine.

Get The Application Working

These are the steps to get the application working.

  • Fill out the code example form. We will send you an email where you can download the source code.
  • Inside the source code, there is a folder called Database. Within that folder, there is a file called FileLogger.bak. This needs to be imported as a database into your SQL Server.

From there, you will need to open up the RoundTheCode.FileLogger/Web/RoundTheCode.FileLogger.Api/appsettings.json file and change this setting:

  • Check the database connection is correct in ConnectionStrings > FileLoggerDbContext.

You are now ready to go.

Open the Project in Visual Studio

Open up RoundTheCode.FileLogger.sln in Visual Studio. Make sure that the project RoundTheCode.FileLogger.Api is set as the start up project.

Start the project in Visual Studio.

You should find that the Web API runs on https://localhost:3510.

Open up a browser and navigate to https://localhost:3510.

If you now go to RoundTheCode.FileLogger/Web/RoundTheCode.FileLogger.Api folder, there should have been a logs folder created.

Inside there, a log file should have been created.

More Information

You can read our Create Your Own Logging Provider to Log to Text Files in .NET Core article for more information on this subject.

In-addition, you can watch our live stream that we did on the subject.