How to save log4net log into database

How to save log4net log into database || How to use log4net in .net  ||What is Log4net


Log4net is an open source .net library to log output to a variety of sources  like console, SMTP, Plain text, Database etc. Log4net is a port of the popular log4J library used in Java. Log4net developed by Apache Foundation. Full details of log4net can be found at its project homepage. Its powerful tool to logging application output to different targets. Log4net provide different types of provider to save logging into plain text, database etc.  Log4net enable logging at runtime without modifying the application binary. Its provide high speed of logging.

Log4net is the notion of hierarchical loggers log4net is designed for speed and flexibility

Home page of log4net- http://logging.apache.org/log4net/index.html

Following is steps to save log4net log into database

1. Add Log4net.dll into your project
2. Add log4net into Global.asax.cs
3. Register log4net in configSections
 4. Use log4net configuration in web.config sections
 5. Create a database and table to save log into Sql server. I am creating "Log4NetTest" database and "AppLog" table to save log into database
6. Use following Log4net settings in your controller, where you want use log4net


Step 1 . Add Log4net.dll into your project

  

 Add log4net "log4net.dll" library into your project. you can download log4net binary from following URL

https://logging.apache.org/log4net/download_log4net.cgi

Its contains different bin file for different .net version. You can add log4net.dll according to your requirement and .net version.

Or

Package Manager - You can install log4net via package manager

Go to Tools>>NuGet Package Manager >> Package Manager
then run

PM> Install-Package log4net






Step 2. Add log4net into Global.asax.cs


Add following code into Global.asax.cs file on "Application_Start()" event.

log4net.Config.XmlConfigurator.Configure();

 

Step 3. Register log4net in configSections

Add following code into configSections in web.config

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





Step 4. Use log4net configuration in web.config sections

Add following code below configSections in web.config

Set your database connection string in "connectionString"



?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<log4net>
   <!--AdoNet appender is use for write log file into sql server-->
   <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      />
      <connectionString value="data source=[SqlServer];Initial Catalog=[DatabaseName];user id=sa;password=[System12345];"
      />
      <commandText value="INSERT INTO AppLog ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message,
@exception)" />
      <parameter>
         <parameterName value="@log_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="@log_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>
      <parameter>
         <parameterName value="@exception" />
         <dbType value="String" />
         <size value="2000" />
         <layout type="log4net.Layout.ExceptionLayout" /> </parameter>
   </appender>
   <!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
   <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
      <!--Enable this line if you want write log file into sql server-->
   </root>
</log4net>
<log4net>
   <!--AdoNet appender is use for write log file into sql server-->
   <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      />
      <connectionString value="data source=[SqlServer];Initial Catalog=[DatabaseName];user id=sa;password=[System12345];"
      />
      <commandText value="INSERT INTO AppLog ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, 

@exception)" />
      <parameter>
         <parameterName value="@log_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="@log_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>
      <parameter>
         <parameterName value="@exception" />
         <dbType value="String" />
         <size value="2000" />
         <layout type="log4net.Layout.ExceptionLayout" /> </parameter>
   </appender>
   <!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
   <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
      <!--Enable this line if you want write log file into sql server-->
   </root>
</log4net>


Step 5. Create a database and table to save log into sql server. I am creating "Log4NetTest" database and "AppLog" table to save log into database

 --Sql Script for save log4net log into sql server database

?
1
2
3
4
5
6
7
Create database Log4NetTest
Use Log4NetTest
CREATE TABLE[dbo].[AppLog](
   [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](max) NOT NULL, [Exception][varchar]
   (max) NULL) ON[PRIMARY] TEXTIMAGE_ON[PRIMARY]
Create database Log4NetTest
Use Log4NetTest
CREATE TABLE[dbo].[AppLog](
   [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](max) NOT NULL, [Exception][varchar]
   (max) NULL) ON[PRIMARY] TEXTIMAGE_ON[PRIMARY]


 Select * from AppLog

Step 6. Use following Log4net settings in your controller, where you want use log4net

?
1
2
3
4
5
6
7
8
9
private static log4net.ILog Log {
   get;
   set;
}
ILog log = log4net.LogManager.GetLogger(typeof(classtype)); //type of class
log.Debug("Debug message");
log.Warn("Warn message");
log.Error("Error message");
log.Fatal("Fatal message");
private static log4net.ILog Log {
   get;
   set;
}
ILog log = log4net.LogManager.GetLogger(typeof(classtype)); //type of class
log.Debug("Debug message");
log.Warn("Warn message");
log.Error("Error message");
log.Fatal("Fatal message");
 


Previous
Next Post »
Thanks for your comment