In this article we will learn how to log an error using Log4Net in a web application.
IntroductionIn this article we will see how to log errors using Log4Net in a web application.Step 1Open Visual Studio 2013.Step 2Go to the "File" menu then select "New" and click on "Project...".Step 3Select "Web" from the left panel, then "ASP.NET Web Application".Provide an appropriate application name and select the Location.Press the "OK" button.Step 4Select "Empty" and check the "Web Forms" check box.Press the "OK" button.Step 5Right-click on the project then select "Add" and click on "New Folder".Step 6Provide the folder name as "Lib". This folder is used to copy Log4Net.dll.Step 7Copy "log4net.dll" to the Lib folder. You can find this DLL in the downloads. Step 8In order to add a reference, right-click on "References" and select "Add Reference".Step 9Select "log4net.dll" from the Lib folder.Step 10The reference is added under "References". Step 11Add the following code snippet to the web.config file.Step 12In the web.config file, we set the file value as "LogBackUp". So create a folder with the same name in the root directory.Right-click on the project, expand "Add" and click on "New Folder".Step 13Name it "LogBackUp".Step 14Configure log4net in the Application_Start event.Step 15Now right-click on the project, select "Add" and click on "New Item".Step 16Select "Web Form", provide an appropriate name and click on the "Add" button.Step 17Add the following code snippet to the "WebForm1.aspx.cs" file. Here we throw an exception intentionally to catch the exception and log it.Step 18Run the application and it creates a log file under "LogBackUp". Step 19Open the .Err file and you can see that the exception message is successfully logged.
ASP.NET MVC 5: A Beginner’s Guide