Shopping Cart Application Part-03 (Logging)

By | April 10, 2015

In my previous article of this series I discussed about create a infrastructure for different configuration settings
Now we’ll add logging concerns inside infrastructure project.
@. Download Log4Net inside Infrastructue from Nuget with following command:

Install-Package log4net

@. The logging mechanism works in the exact same way as the application setting functionality.
@. Add an interface named ILogger inside Logging folder with following definition:

namespace Shoppingcart.Infrastructure.Logging
{
    public interface ILogger
    {
        void Log(string message);
    }

}

@. Now create a factory class named LoggingFactory with following definition:

namespace Shoppingcart.Infrastructure.Logging
{
    public class LoggingFactory
    {
        private static ILogger _logger;

        public static void InitializeLogFactory(ILogger logger)
        {
            _logger = logger;
        }

        public static ILogger GetLogger()
        {
            return _logger;
        }
    }
}

@. Implement ILogger interface and named the concrete class as Log4NetAdapter with following definition:

public class Log4NetAdapter : ILogger
{
        private readonly log4net.ILog _log;
        public Log4NetAdapter()
        {
            XmlConfigurator.Configure();
            _log = LogManager.GetLogger(ApplicationSettingsFactory.GetApplicationSettings().LoggerName);
        }
        public void Log(string message)
        {
            _log.Info(message);
        }
}

@. Our implementation of ILogger i.e. Log4NetAdapter usese Adapter pattern that uses to “Converts the interface of a class into another interface clients expect”
@. Here Log4NetAdapter class uses the ApplicationSettingsFactory to obtain the logger name, which determines the logging strategy that log4net will use.
@. Logging strategy configured inside web.cofig as follows:
@@. Inside Config Section of Web.Config add following line of code:

So inside config section it will look like


    

@@. Now we’ll define a new section named log4Net inside Web.Config as follows:

 
		
			
			
		
		
			
			
			
			
			
			
			
				
			
		
	

That’s all for Logging. In my next article i will discuss about EmailSetting

One can download entire source from here

485 Total Views 1 Views Today
Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
https://www.scrumalliance.org/community/profile/mhaque13

Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Category: Asp.net MVC Learn by doing - ASP.Net MVC Tags: , , , ,
Md. Mojammel Haque

About Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org) Certification Profile Link- https://www.scrumalliance.org/community/profile/mhaque13 Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Leave a Reply

Your email address will not be published. Required fields are marked *