.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.
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.
This is the software that will need to be installed onto your machine.
- Visual Studio 2019. Version 16.8.2 or above. It will work with the free community version.
- SQL Server 2017, or above.
- .NET 5.0 SDK, or above.
Get The Application Working
These are the steps to get the application working.
- Fill out the Download the Source Code 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
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
Open up a browser and navigate to
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.
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.