If you developer in C# and you want create a log file for save errors or any important info, this blog post is for you. In this blog you will get the idea how to create simple log file in C#.
Why We Need Log File?
When we perform coding operation using any developing tool like VS Code OR VS 2022 or any other, during development and debugging process you make mistake, it show you error right there where mistake is. This help when you coding and testing by yourself.
But at time of uploading your code in UAT server or in Preview server, not on your computer. Now, on server there is no errors pop up. Here, log file become super important. When code run on server, log file keep track of what happening, means at which point of line your code executed successfully.
If something go wrong during execution, you can look at log file. This way, even if you are not there nor any developing tool, you still know where your code having trouble.
How to Implement NLog in ASP.NET Core
What is NLog?
NLog is a logging library for .NET and you can install it in your project either using package console or using Nuget. It helps you to log or write down what’s happening in your application. It’s like keeping a diary for your application, noting down what it does, especially when something goes wrong or execution of your code.

Right click on your project name for me here it is InstallNLogSample. And Select Manage Nuget Packages. Check the Image below make sure you have selected the Browse Tab and search NLog. Click on Install button. From the Pop-up window select Apply.


After successfully installation expand the Package from your project to see the NLog is installed or not. Always do one thing after adding new packages – build your project. It’s like checking your work to make sure everything is set right.

How to create LogError Class in C# | Create Simple Log file
As a developer it is always a good practise to centralized your class and method which can be later used by other controller also. I am developer so I will guide your exactly the same way how developer create simple log file.
First create as class inside your Model Folder with the name

How to add .config file in .NET Core?
Right click on your project and select add and then New Item.

Select show all template if you are unable to see the below pop-up window. Give the File name as nlog.config.

When file gets generated paste the below code in it.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true"
throwExceptions="false" internalLogFile="D:\NLogErrors\log.txt">
<targets>
<target name="console" xsi:type="ColoredConsole" layout="${message}" />
<target name="file" xsi:type="File" fileName="${basedir}/logs/${date:format=dd-MM-yyyy}.log" layout="-- ${level}(${longdate}) -- ${exception:format=Type} -- ${exception:format=Message}-- ${exception:format=Stack Trace}-- ${message}${newline}"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
</rules>
</nlog>
Now it is time to use above error message/info function to display our log file.

You want see where log files go? You check line 12 in TestController. It will guide you where files should be. Now, you go browser and run your TestController. Then, you look in the debug folder, inside there should be a Logs folder. contains your file based on format which you have specified in the NLog.config file. (D:\Personal\Training\InstallNLogSample\InstallNLogSample\bin\Debug\net8.0\logs)

Open your file you will see it contains information regarding date time and the message.

If you are interested to download the copy of this project from GitHub link :- InstallNLogSample
