Steps
To use Apache log4net in project for error handling
- The
first step for using apache log4net is to download DLL of apache log4net.
You can
download it from link http://logging.apache.org/log4net/download.html
- The second step is to add reference of this DLL in our project where we want to use apache for error handling.
- The third step is to open the web config and add the following code in the webconfig
----------------------------------------------------------------------------------------------------------
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="D:\anuj.log"/>
<appendToFile value="true"/>
<maximumFileSize value="1024KB"/>
<maxSizeRollBackups value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%date %level
%logger - %message%newline"/>
</layout>
</appender>
<root>
<level
value="DEBUG"/>
<appender-ref
ref="RollingFile"/>
</root>
</log4net>
------------------------------------------------------------------------------
<appender
name="RollingFile" type="log4net.Appender.RollingFileAppender">
An appender defines HOW log4Net is going to log
application information.
<appendToFile
value="true"/>
Sets a flag that
indicates whether the file should be appended to or overwritten. So, for
example, if it's set to '
true' and the application is stopped and
started, then log4Net will attempt to reuse the same log file.
<maximumFileSize value="1024KB"/>
Sets the maximum
size that the log file is allowed to reach before being rolled over to backup
files.
<maxSizeRollBackups value="10"/>
Sets the maximum
number of backup (rollover) files that are kept before the oldest is erased.
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%date %level
%logger - %message%newline"/>
</layout>
Formats the logging
event as a
string.
<level value="All"/>
Defines the logging
level for application events, e.g.
ERROR, DEBUG, INFO.
And this code in <configsection>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
- The forth step to add following code in the
form
Imports System.Data
Imports System.Data.SqlClient
Imports System
Imports System.Collections.Generic
Imports log4net
Imports log4net.Config
Partial Public Class _Default
Inherits
System.Web.UI.Page
Protected Shared ReadOnly log As ILog =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles Me.Load
log4net.Config.XmlConfigurator.Configure()
log.Debug("Hello
World!")
log.Info("I'm
a simple log4net tutorial.")
log.Warn("...
better be careful ...")
log.Error("ruh-roh:
an error occurred")
log.Fatal("OMG
we're dooooooomed!")
End Sub
End Class
We can also confiure error log file
in global.asax file.Insted of configring in all the forms
In Case of multiple project In a
single soution You just have to add reffrence in that project and create object
for log4net.And you can simply use it as in given example
Imports System
Imports
System.Data
Imports log4net
Imports
log4net.Config
Public Class Class1
Protected Shared ReadOnly log As ILog =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Public Sub log4net()
log.Error("Form1_Load()
- ")
log.Warn("Anuj
is great")
log.Warn("I
m In class1 of view")
End Sub
End Class
This will create error log file in
specified loction in Web config
No comments:
Post a Comment