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
CREATE TABLE[dbo].[Log]([Id][int]IDENTITY(1,1)NOTNULL,
[Date][datetime]NOTNULL,[Thread][varchar](255)NOTNULL,[Level][varchar](50)NOTNULL,
[Logger][varchar](255)NOTNULL,[Message][varchar](4000)NOTNULL,
[Exception][varchar](2000)NULL)
1. Download log4net from http://logging.apache.org/log4net/download.html

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.

<configsections>

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

</configsections>

<log4net>

<root>

<level value="DEBUG">

<appender-ref ref="ADONetAppender">

</root>

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

<buffersize value="100">

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

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

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

<parameter>

<parametername value="@date">

<dbtype value="DateTime">

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

</parameter>

<parameter>

<parametername value="@thread">

<dbtype value="String">

<size value="255">

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

<conversionpattern value="%thread">

</layout>

</parameter>

<parameter>

<parametername value="@level">

<dbtype value="String">

<size value="50">

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

<conversionpattern value="%level">

</layout>

</parameter>

<parameter>

<parametername value="@logger">

<dbtype value="String">

<size value="255">

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

<conversionpattern value="%logger">

</layout>

</parameter>

<parameter>

<parametername value="@message">

<dbtype value="String">

<size value="4000">

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

<conversionpattern value="%message">

</layout>

</parameter>

</appender>

</log4net>

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;
try
{
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);

log4net.Config.XmlConfigurator.Configure();

log.Debug(ex.Message);

log.Error(ex.Message);

log.Warn(ex.Message);

log.Info(ex.Message);

log.Fatal(ex.Message);
}
}
}

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

Powered by Blogger.