.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.
These are the steps to get the application working.
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:
ConnectionStrings > FileLoggerDbContext.
You are now ready to go.
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.