7

Un-tangling Sitecore configuration includes

 3 years ago
source link: https://blog.coates.dk/2016/03/15/un-tangling-sitecore-configuration-includes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

I recently worked on a project that used SlowCheetah (XML Transforms) and Octopus variable substitution to modify the custom Sitecore include files.

It proved difficult to determine what the Sitecore configuration was in each environment, especially for the content delivery servers, as it was not possible to call showconfig.aspx.

Solution

Each time the application starts, it writes out the contents of the merged Sitecore configuration to a file in the logs folder. The file name contains the instance name, date and time created. So in addition to seeing the current configuration, you can also see how it changes over time (very useful after a deploy where nothing works).

Get the merged Sitecore configuration

It turned out to be very simple to implement, as it only takes one line of code to get the merged Sitecore configuration:

XmlDocument xmlDocument = Sitecore.Configuration.Factory.GetConfiguration();

Create a custom pipeline processor class

Create a processor for the initialize pipeline, so each time Sitecore is started the processor will be called to ensure that the configuration is saved. Create a public class with a public member called Process, which accepts a parameter of type PipelineArgs. The code below is all that is needed.

namespace Exmaple
{
public class SaveSitecoreConfiguration
{
public void Process(PipelineArgs args)
{
string fullPath=string.Empty;
try
{
XmlDocument configuration = Factory.GetConfiguration();
string filename = string.Format("SitecoreConfiguration.{0}.{1}.xml", DateTime.Now.ToString("yyyyMMdd-hhmm"), Sitecore.Configuration.Settings.InstanceName);
string logFolder = Sitecore.Configuration.Settings.LogFolder;
// Is it a relative or virtual folder ?? could be a configured to point at an physical directory
if (!Directory.Exists(logFolder))
{
logFolder = HttpContext.Current.Server.MapPath(logFolder);
}
fullPath = Path.Combine(logFolder, filename);
configuration.Save(fullPath);
}
catch (System.NotSupportedException supportedException)
{
Sitecore.Diagnostics.Log.Error(string.Format("Error saving sitecore configuration, path:{0}", fullPath), supportedException, this);
}
catch (Exception exception)
{
Sitecore.Diagnostics.Log.Error("Error saving sitecore configuration", exception, this);
}
}
}
}

Configuration Changes

The processor has to be added to the initialize pipeline, I would recommend you create an include file to achieve this, but for the sake of clarity I have added it directly to the web.config, see below.

example

Now every-time Sitecore is started it writes out the configuration, so it is easy to get the configuration and monitor how it changes for all environments over time.

I hope this helps you untangle the Sitecore includes which at times can be a nightmare.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK