Log4net is an Open Source utility used for log/report statements to various kinds of output targets Namely.
- TextFile
- EventViewer
- SQL Server
- Email
Integrating Log4net in simple 4 steps
Step 1: Add a reference of Log4net.dll to the project.
Step 2: Add Global.asax to the project and add the following code:
log4net.Config.XmlConfigurator.Configure();
a. under Configuration->Configsections, add
<section name="<span class=" span="" class="hiddenSpellError" pre="class ">hiddenspellerror="" pre="">log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></section>
b. Add a new section ““ & add the required appenders (output target) configuration sections in the following way:
<configSections>
<section name="<span class=" hiddenspellerror="" pre="">log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></section>
</configSections>
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\\Logs\\TestLog.log"/>
<layout type="log4net.Layout.PatternLayout">
</layout>
</appender>
:
:
<logger name="File">
<level value="All"/>
<appender-ref ref="LogFileAppender"/>
</logger>
</log4net >
Step 3: call the appropriate method in the class
.cs file where we log
public partial class HierarchicalMenuDemo : System.Web.UI.Page
{
//for logging to file
private static readonly ILog log = log4net.LogManager.GetLogger("File");
private static readonly ILog evlog = log4net.LogManager.GetLogger("EventViewer");
private static readonly ILog sqllog = log4net.LogManager.GetLogger("SQLServer");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XmlDataSource1.Data = GetMenuData();
}
log.Info("Starting page load");
log.Debug("This a test debug message");
log.Error("Page Load failed : ");
evlog.Info("Starting page load");
evlog.Debug("This a test debug message");
evlog.Error("Page Load failed : ");
sqllog.Info("Starting page load");
sqllog.Debug("This a test debug message");
sqllog.Error("Page Load failed : ");
}
}
Step 3: Use Database script
If you want to target the logging information in the SQL Server use the following script create Table in the existing database.
CREATE TABLE [dbo].[Log] ( [Id] [int] IDENTITY (1, 1) NOT NULL, [Date] [datetime] NOT NULL, [Thread] [varchar] (255) NOT NULL, [Level] [varchar] (50) NOT NULL, [Logger] [varchar] (255) NOT NULL, [Message] [varchar] (4000) NOT NULL, [Exception] [varchar] (2000) NULL )
OUTPUT Screens
Download log4Net:
http://logging.apache.org/log4net/download.html
The Sample uses NorthWind Database download it from :
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&displaylang=en
Pingback: How to use log4net for logging in ASP.Net «
looks wonderful!