0

I've a Web API application. In the solution of the application, there are several projects.

And all the API's are in one single project. And there is one project for business layer.

We want to write one logging class containing all the relevant methods in the business layer project and we are going to use "Enterprise Library Logging Block".

What is the correct procedure to get the related configuration from the web.config in the class of the business layer project.

Thanks in advance.

Arnab Das
  • 3,548
  • 8
  • 31
  • 43

1 Answers1

0

If the configuration is contained in web.config and the business layer assembly executes in the same appdomain as the Web API application then the only thing you will need to do is bootstrap the blocks you are using (in this case it sounds like just logging).

You could do this at application startup (e.g. App_Start):

Logger.SetLogWriter(new LogWriterFactory().Create());

In this approach the Business Layer will use the static facade Logger.Write to write LogEntries.

A better approach would be to create a small wrapper around a LogWriter that will bootstrap the block and expose the LogWriter for use in the business layer (and anywhere that needs logging). This is a bit more friendly for dependency injection since it's easy to register in a container and can be passed in as a dependency.

public class Logger
{
    private Lazy<LogWriter> logWriter = new Lazy<LogWriter>(() =>
        {
            LogWriterFactory factory = new LogWriterFactory();
            return factory.Create();
        });

    public LogWriter LogWriter
    {
        get { return logWriter.Value; }
    }            
}

Since internally LogWriter is a singleton and you only want to bootstrap once you would probably make the custom Logger class a singleton. (If you don't care about having an instance you could make Logger entirely static.)

Randy Levy
  • 22,566
  • 4
  • 68
  • 94