Using Enterprise Library in SharePoint for Logging and Exception Management – Part 1

In this post I will detail how to make the Enterprise Library (EntLib) reads from config files other than the web.config so you can use the EntLib in your SharePoint application without having to modify the web.config of the SharePoint web application.

Part 1 will deal with logging. Part 2 in another post will deal with exception management.

Using the EntLib is a good way to do logging and exception management as it allows you configure these aspects for your application and dynamically change them without having to rewrite code. Using the EntLib for logging and exception management is often considered a best practice.

By default the EntLib relies on the application’s config file. This can be a problem in SharePoint because this means you have to change the web.config at the web application that is hosting the site collection. This has a number of drawbacks, including complicating your deployment process, hard to separate out the configuration for unrelated parts in your SharePoint site, and having to deal with that SharePoint admin dude who is rarely available to help you.

One way to overcome this is to tell EntLib to read from other config files rather than the web.config. This way you can include a config file (say logging.config) with your feature and deploy it to your feature folder without needing to touch the main web.config file.

So for logging – first create a method to create a LogWriterFactory base on a given configuration path.

public static GetLogWriterFactory(string configurationFilePath)
{
  IConfigurationSource configSource = null;
  if (configurationFilePath == null || configurationFilePath == String.Empty)
  {
    //Use the default application’s config (i.e. web.config or app.config) if the file path
    //is not specified.
    configSource = new SystemConfigurationSource();
  }
  else
  {
     configSource = new FileConfigurationSource(configurationFilePath);
  }
  return new LogWriterFactory(configSource);
}

Then to do the logging:

LogWriter writer = GetLogWriterFactory(“c:\someFolder\someFileName.config”);

//From here is the normal code to create a log entry and write it.
LogEntry entry = new LogEntry();
entry.Message = “Hello World”;
writer.Write(entry);

Use the EntLib’s configuration editor tool to create the someFileName.config file.

So in summary – to configure EntLib logging using a config file other than the web.config or app.config:

  • Use the FileConfigurationSource class to point EntLib to the config source.
  • Use the LogWriterFactory class to create a LogWriter using the FileConfigurationSource.
Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Config, Enterprise Library, Exception Management, Logging, SharePoint. Bookmark the permalink.

One Response to Using Enterprise Library in SharePoint for Logging and Exception Management – Part 1

  1. Jose Miguel says:

    Hi, my name is Jose Miguel, from Spain.
    I have a curious problem with an aspx web application and sharepoint 2013. My web application is hosted on IIS 8 on a site with its own web.config. The application also has a CustomMembershipProvider against LDAP configured in the web.config of the site, central administration and STS, as said MSDN documentation.
    The problem is that when debugging the application and by EntLib I try to create a BBDD gives me an error stating that the BBDD is not configured …. the problem is that the application is using the web.config of STS and not the one of its site. Does it sound like something to you?

    Thanks in advance.
    Best regards.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s