Insights

Logging Publishing In Sitecore

Logging Publishing Events In Sitecore

It doesn't happen often, but every now and then it's useful to know precisely what is being published. Perhaps, from an auditing perspective, you want to have an auditable log of that information.

Ever wonder what gets published when you Publish subitems or Publish related items? Now you can find out. So let's do it.

How to log publishing events in Sitecore

Publishing Event

There are undoubtedly many ways to go about capturing this information. What I want to show you is but one way. In this case, when we think about the publishing process, we know that there are several events related to publishing. So naturally, this way is about attaching a processor to that cycle. We can see in the showconfig.aspx the one we want is called publish:itemProcessed. If we waited until publish:end we would not be able to get the individual item information we want.

I've broken down the code below so you understand what's going on. You need to be aware though, this can be an expensive process to run. Especially if you know you're always publishing huge volumes of information. So with that in mind, you may care to disable the configuration for it until you need it.


using System;
using log4net;
using Sitecore.Publishing.Pipelines.PublishItem;

namespace App.Foundation.Publishing.Processors
{
    class ItemProcessedProcessor
    {
        public void ItemProcessed(object sender, EventArgs args)
        {
            // 1. Convert the arguments into appropriate ItemProcessedEventArgs
            ItemProcessedEventArgs itemProcessedEventArgs = args as ItemProcessedEventArgs;

            // 2. Connect to the publishing context
            PublishItemContext context = itemProcessedEventArgs != null ? itemProcessedEventArgs.Context : null;

            if (context == null) return;

            // 3. Set up custom logging or log to the existing Publishing log file
            ILog log = Sitecore.Diagnostics.LoggerFactory.GetLogger("Sitecore.Diagnostics.Publishing");

            // 4. Setup the database
            Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");

            // 5. Identify the item being processed
            Sitecore.Data.Items.Item item = master.GetItem(context.ItemId);

            // 6. Log any relevent information
            if (item != null)
            {
                log.Info("**** App - Publishing Item: " + item.ID.ToString() + " " + context.Result.Operation.ToString() + " " + item.Paths.Path.ToString());
            }
        }
    }
}

Configuration

Having the processor setup is but half the solution. We need to add it to the overall Sitecore configuration. So let's do that now.

Here we've created a patch file to add an event handler within the itemProcessed event logic.


<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv">
    <sitecore role:require="Standalone or ContentDelivery or ContentManagement">
        <events>
            <event name="publish:itemProcessed">
                <handler type="App.Foundation.Publishing.Processors.ItemProcessedProcessor, App.Foundation.Publishing" method="ItemProcessed">
                </handler>
            </event>
        </events>
    </sitecore>
</configuration>

And that's it. Super easy. You can now see what gets published when you publish sub items or related items.

Hey, Developers!

We're on the look out for talented developers to join our team.

Think you have what it takes?

Meet David Austin

Development Team Lead

📷🕹️👪

David is a decorated Development Team Lead with Sitecore Technology MVP and Coveo MVP awards, as well as Sitecore CDP & Personalize Certified. He's worked in IT for 25 years; everything ranging from Developer to Business Analyst to Group Lead helping manage everything from Intranet and Internet sites to facility management and application support. David is a dedicated family man who loves to spend time with his girls. He's also an avid photographer and loves to explore new places.

Connect with David