Today, we will learn these topics.
- How to use Exception filter?
- How to configure Log4Net in webapi 2?
- How to create Global Exception filter?
- How to avoid to use try catch in application?
- How to create log file with log details in drive.
- Advantage of Exception filter.
To learn these topics, you should follow steps which are given below.
Step 1 - Create a web based application in MVC or Web API.
As per your choice, you can name your project. I have named it "swaggerTsting". Click on "OK". Thereafter, the next window will open. Select "MVC" OR "WebAPI" or both and click on OK.
After that, you can see your project in Solution Explorer.
Here, you can see some API Controllers already created with demo output. You can create any number of Controllers and Actions. If I run this application, it will run correctly.
Now, I am going to integrate Exception Filter with log4net. So, follow some steps....
Step 2 - First, add log4net in your Solution, using NuGet Package or PM console. I am going to add log4net by PM console.
Thereafter, click on "Package Manager Console ". You will see a new window on the bottom of editor, as shown below.
Just type "Install-package log4net" or copy paste .
PM> Install-Package log4net
Click enter and wait for 30 seconds. The log4net file will be added in your Solution. You can check that in Solution Explorer .
In this image, you can see that log4net is added and showing message in bottom pane.
- log4net.config
- ExceptionManagerApi.cs
You can see it in the following image.
Step 4 - Write custom code inside log4net.config file to set the log path, log format, and log type etc.
Log4net - Copy this code and paste in your log4net.config file.
- <log4net>
- <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
- <!--
- The file location can be anywhere as long as the running application has read/write/delete access.
- The environment variable also can be set as the location.
- <file value="${TMP}\\Log4NetTest.log"/>
- -->
- <file type="log" value="D:\Logger.log"/>
- <appendToFile value="true"/>
- <rollingStyle value="Size" />
- <maxSizeRollBackups value="5" />
- <maximumFileSize value="5MB" />
- <!--Ensure the file name is unchanged-->
- <staticLogFileName value="true" />
- <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
- <layout type="log4net.Layout.PatternLayout">
- <header value="Logging Start
-
- " />
- <footer value="Logging End
-
- " />
- <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
- </layout>
- <!--<layout type="log4net.Layout.PatternLayout">
- <header value="[Header]
-
- " />
- <footer value="[Footer]
-
- " />
- <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
- </layout>-->
- </appender>
-
- <root>
- <!--
- 1.OFF - nothing gets logged
- 2.FATAL
- 3.ERROR
- 4.WARN
- 5.INFO
- 6.DEBUG
- 7.ALL - everything gets logged
- -->
- <level value="ALL"/>
- <appender-ref ref="RollingFile"/>
- </root>
- </log4net>
In this code, you can change log format and destination path to save log details.
Step 5 - Now, go to configure Exception Filter logic in "ExceptionManagerApi.cs". Copy and paste this code in your "ExceptionManagerApi.cs" file.