Configure log4Net for VB.net or C#.net

  1. Download log4Net from apache http://logging.apache.org/log4net/
  2. Add log4Net.dll to your project reference
  3. Add following code to web.config under tab
    <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
        </configSections>
        <log4net>
            <!--Root Logger is for all logger-->
            <!--<root>
                <level value="ALL" />
                <appender-ref ref="FileAppender" />
            </root> -->
            <logger name="FileAppender">
                <level value="ALL" /> Check all the level
                <appender-ref ref=" FileAppender " />
            </logger>
            <appender name=" FileAppender " type="log4net.Appender.RollingFileAppender">
                <file value="[Your directory and file name where you want to store log like ‘C:/Temp/app.log’]" />
                <appendToFile value="true" />
                <rollingStyle value="Composite" />
                <datePattern value="yyyyMMdd" />
                <maxSizeRollBackups value="10" />
                <maximumFileSize value="200K" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] - [%logger] %message%newline" />
                </layout>
            </appender>
        </log4net>
    
    

    FileAppender is the name of the logger class, all logger with the name FileAppender will follow this configuration.
    *file value set the store path of the log file.

  4. Create a Logger class
    VB.NET

    Imports log4net
    
    Public Class Logger
        Private Shared logger As ILog = LogManager.GetLogger("FileAppender")
    
        Public Shared Sub LogInfo(ByVal str As String)
            logger.Info(str)
        End Sub
    
        Public Shared Sub LogError(ByVal str As String)
            logger.Error(str)
        End Sub
    
        Public Shared Sub LogFatal(ByVal str As String)
            logger.Fatal(str)
        End Sub
    End Class
    
    

    C#.NET

    using log4net;
    
    public class Logger
    {	
    private static ILog logger = LogManager.GetLogger("FileAppender");
    	public static void LogInfo(string str)
    	{
    		logger.Info(str);
    	}
    
    	public static void LogError(string str)
    	{
    		logger.Error(str);
    	}
    
    	public static void LogFatal(string str)
    	{
    		logger.Fatal(str);
    	}
    }
    
    
  5. Import the Logger class to your class and use it anywhere in your project
    Logger.LogInfo
    Logger.LogError
    Logger.LogFatal
    

I will add the sample project for this soon.

2 thoughts on “Configure log4Net for VB.net or C#.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s