Thursday, August 18, 2016

Log4Net


log4net step by step


1. Install Log4Net either by adding log4Net.dll or by running below command,
  • PM> Install-Package log4net

2. Create XML file inside web application project as below

<? xml version = " 1.0 " ?>
< log4net >
< appender name = " FileAppender " type = " log4net.Appender.RollingFileAppender " >
< param name = " File " value = " D:\LogFile.log " />
< param name = " DatePattern " value = " yyyy.MM.dd " />
< param name = " RollingStyle " value = " Size " />
< param name = " maxSizeRollBackups " value = " 10 " />
< param name = " maximumFileSize " value = " 100KB " />
< layout type = " log4net.Layout.PatternLayout " >
< param name = " ConversionPattern " value = " %date{yyyy-MM-dd HH:mm:ss ffff} [%-3t] %-5p %logger{1}.%method: %message%newline " />
</ layout >
</ appender >
< root >
<!-- OFF, FATAL, ERROR, WARN, DEBUG, INFO, ALL -->
< level value = " ALL " />
< appender-ref ref = " FileAppender " />
</ root >
</ log4net >

Assume that "log4net.xml" is the file name of the above created xml file

3.  Configure above created xml file in web.config
  • In <appsettings> section add below entry
  • <add key = "log4net.config" value = "log4net.xml" />
4. Create Logger.cs in project and add reference of log4net.dll in the project. Mostly this would be in the utility project as we can refer utility project in any of the layer/project.

Using System;

namespace yournamespacename;
public class logger
{
private static log4net.ILog Log {get; set;}
static Logger
{
Log = log4net.LogManager.GetLogger(typeof(Logger));
}

public static void Error (Object message)
{
Log.Error(message);
}

public static void Error (Object message, Exception Ex)
{
Log.Error(message, Ex);
}

public static void Error (Exception Ex)
{
Log.Error(Ex);
bool x = Log.IsErrorEnabled;
}

public static void Info (Object message)
{
Log.Info(message);
}

}

5. Make sure the user (generally Iuser) configured in IIS to access the website has read write access to the Log file. This can be done by right click -> properties -> Security -> Edit -> Add -> select the user and give the permissions.

6. Below is the sample which can be used in catch block
  • Add reference of above project where log4net has to be used
  • Catch block can be as below
Catch (exception ex)
{
Logger.Error(ex);
}

 ====================================================================
If it is not working, follow below changes,

1. Add below line in AssemblyInfo.cs file,
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

2. Add below section in App.config file,

<configSections>

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

</configSections>

<log4net>

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

<param name="File" value="D:\\PUDLog.log" />

<AppendToFile value="true" />

<param name="DatePattern" value="yyyy.MM.dd" />

<param name="RollingStyle" value="Size" />

<param name="maxSizeRollBackups" value="10" />

<param name="maximumFileSize" value="100KB" />

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

<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss ffff} [%-3t] %-5p %logger{1}.%method: %message%newline" />

</layout>

</appender>

<root>

<!-- OFF, FATAL, ERROR, WARN, DEBUG, INFO, ALL -->

<level value="DEBUG" />

<appender-ref ref="FileAppender" />

</root>

</log4net>

No comments:

Post a Comment