Search This Blog

Making Log for your application / Website

Making Log for your application / Website


 


 

Now it is easy to create log for your application or website or function.

Here I am explaining the way of making log in text file using log4net dll .

Step 1


 

Download log4net 1.2.10 (zip) from http://logging.apache.org/log4net/download.html.


 

Step 2

Get the dll file from path ~\log4net-1.2.10\bin\net\2.0\debug\ log4net.dll


 

Step 3

Add this dll to your project folder and give it's reference to the project.


 

Step 4

Create one Configuration File as below and name it. (here LogConfigure.config)


 

<?xml
version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section
name="log4net"


type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

</configSections>

<log4net>

<appender
name="LogFileAppender" type="log4net.Appender.FileAppender">

<param
name="File" value="LOGGING.txt" />

<param
name="AppendToFile" value="true" />

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

<param
name="ConversionPattern" value="%d [%t] %-5p %m%n" />

</layout>

</appender>

<root>

<level
value="INFO" />

<appender-ref
ref="LogFileAppender" />

</root>

</log4net>

</configuration>


 

Step 5

Make one class as below and name it. (here LoggerNet.vb)


 

Imports log4net

Imports log4net.Config

Imports System.IO

Class
LoggerNet


Private
ReadOnly ilog As
ILog = LogManager.GetLogger(GetType(SharePointLoader))


Public
Sub
New()


Dim fno As
FileInfo = New
FileInfo("LogConfigure.config")


XmlConfigurator.Configure(fno)


End
Sub


Public
Sub LogInfo(ByVal Message As
String)

ilog.Info(Message)


End
Sub


Public
Sub LogError(ByVal Message As
String)

ilog.Error(Message)


End
Sub


Public
Sub LogError(ByVal Message As
String, ByVal ex As
Exception)

ilog.Error(Message, ex)


End
Sub

End
Class


Or C# Code as below LoggerNet.cs

using log4net;

using log4net.Config;

using System.IO;

class LoggerNet

{

    private readonly ILog ilog = LogManager.GetLogger(typeof(SharePointLoader));

    public LoggerNet()

    {

        FileInfo fno = new FileInfo("LogConfigure.config");

        XmlConfigurator.Configure(fno);

    }

    public void LogInfo(string Message)

    {

        ilog.Info(Message);

    }

    public void LogError(string Message)

    {

        ilog.Error(Message);

    }

    public void LogError(string Message, Exception ex)

    {

        ilog.Error(Message, ex);

    }

}

Step 6

Now from every class file where you want to create log just add the below code.

Create Static Object of LoggerNet

VB    Private
Shared ilog As
New
LoggerNet()

C# private static LoggerNet
ilog = new LoggerNet();


 

For information logging


 

Add the code from where you want to log information


 

VB    ilog.LogInfo("Information")


 

Add the code from where you want to log Error


 

VB    ilog.LogError("Error Message")


 

Add the code from where you want to log Exception


 

VB    ilog.LogError(exception.Message, exception)


 

Step 7

This will create a log file named "LOGGING.txt"
(As per config file)

Tip

At the end of application you can rename your file with a Unique name so that there is a unique log for each instance.

File.Move("LOGGING.txt","Unique Name")


 

Link Within Related Posts Plugin for WordPress, Blogger...
There was an error in this gadget