In the last post, part 1, we looked at how to identify who did a publish simply by going into the log file and finding the AUDIT trail for the publish. This would give us information about who did a publish, and what they published.

We did however find that sometimes, under heavy load on the server, the AUDIT trail was suppressed, and we couldn't find the information we wanted. Also system tasks that did publishing, wasn't logged. Therefore we decided to do some custom logging, by hooking into the Publish Processor.

The Publish Processor is a typical processor in Sitecore, and it runs whenever someone does a publish.

In our example, we are logging who did the publish, the root item, and whether its a deep publish(including subitems).

The code is very simple, and looks like the following:

public class PublishLogger : PublishProcessor
 {
    public override void Process(PublishContext context)
    {
        if(context.User != null)
            Sitecore.Diagnostics.Log.Info(string.Format("PublishProcessor: User.Name: {0}", context.User.Name), this);
        if (context.PublishOptions != null)
        {
            if(context.PublishOptions.RootItem != null)
                Sitecore.Diagnostics.Log.Info(string.Format("PublishProcessor: PublishOptions.RootItem: {0}", context.PublishOptions.RootItem.Paths.FullPath), this);
           Sitecore.Diagnostics.Log.Info(string.Format("PublishProcessor: PublishOptions.Deep: {0}", context.PublishOptions.Deep), this);
        }
    }
}

The above code will simply log to the default logging mechanism in Sitecore, but you could put it into a custom log, or just log it to a database, for easier analysis.

The publish processor is configured via a simple .config file in the include folder, it looks like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <publish>
                <processor type="[namespace].PublishLogger,[assemply]" 
                           patch:after="processor[@type='Sitecore.Publishing.Pipelines.Publish.ProcessQueue, Sitecore.Kernel']" />
            </publish>
        </pipelines>
    </sitecore>
</configuration>

There you go, an easy way to log whenever someone does a publish.