store log file in database using log4net - W3TECHNOLOGY

Header Ads

store log file in database using log4net

One of the best ways to store log in the database using log4net coz it is easy to use and its world wide popularity. For storing log in the database all task are manage by log4net internally.

At first you need to create a table for Storing log in the SqlServer database

The table should be like as
1. Download log4net from

2. Open visual studio and create an application.

3. Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution.

4. Now put this web.config/app.config file in configuration tag.


<section type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" name="log4net">




<level value="DEBUG">

<appender-ref ref="ADONetAppender">


<appender type="log4net.Appender.ADONetAppender" name="ADONetAppender">

<buffersize value="100">

<connectiontype value="System.Data.SqlClient.SqlConnection, System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089">

<connectionstring value="server=; uid=sa; pwd=sa; database=databasename">

<commandtext value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@date, @thread, @level, @logger, @message)">


<parametername value="@date">

<dbtype value="DateTime">

<layout type="log4net.Layout.RawTimeStampLayout">



<parametername value="@thread">

<dbtype value="String">

<size value="255">

<layout type="log4net.Layout.PatternLayout">

<conversionpattern value="%thread">




<parametername value="@level">

<dbtype value="String">

<size value="50">

<layout type="log4net.Layout.PatternLayout">

<conversionpattern value="%level">




<parametername value="@logger">

<dbtype value="String">

<size value="255">

<layout type="log4net.Layout.PatternLayout">

<conversionpattern value="%logger">




<parametername value="@message">

<dbtype value="String">

<size value="4000">

<layout type="log4net.Layout.PatternLayout">

<conversionpattern value="%message">





n the connection string tag you need to change server name and database. You also can decide how to define the security part of the connection string.

There are two way to define the security part

· Use integrated Security

· State the username and the password in the connection string.

In both cases you need to make sure that the user has access to the, SQL server, the database and the databasetable that Log4Net is going to use.

If you use integrated security then the connection string should be like as

5. To use log4net put this as a local class variable: private static readonly log4net.ILog log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

6. And do this to write messages in the log file. log.Debug(”this text will be in log file”);

For Example,

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using log4net;

using log4net.Config;

using log4net.Core;

using log4net.Repository.Hierarchy;

using log4net.Appender;

using System.Collections.Generic;

using System.Xml.Schema;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void btn_Click(object sender, EventArgs e)
int a = 23, b = 0;
int result = a / b;
catch (Exception ex)

// Divide by Zero exception occurred and in the next line we are going to log tha

log4net.ILog log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);







Now run the code then you can see the log stored in the database table.

Powered by Blogger.