如何查看完整的发布信息到自定义日志文件中

时间:2015-03-27 08:14:45

标签: sitecore sitecore7.5

我想在Custom log

中创建Sitecore

在此日志文件中,我想在该自定义日志中分隔完整的发布信息

因为我们只能在发布日志文件中看到项目ID,语言版本,项目路径,对于发布用户,我们必须看到标准日志文件

我希望将完整的发布信息看到单独的自定义日志文件中。

谢谢

1 个答案:

答案 0 :(得分:3)

以下代码会有所帮助,您需要在 SiteItem 管道中的 Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics 之后添加处理器,以包含您自己的日志发布信息:

namespace Sitecore.OpenSource.Pipelines.Publishing
{
    public class LogPublicationInfoToCustomLog : PublishItemProcessor
    {
        public override void Process(PublishItemContext context)
        {
            LogToCustomLog(context);
        }

        private void LogToCustomLog(PublishItemContext context)
        {
            Assert.ArgumentNotNull(context, "context");
            Assert.ArgumentNotNull(context.PublishOptions, "context.PublishOptions");
            Assert.ArgumentNotNull(context.PublishOptions.SourceDatabase, "context.PublishOptions.SourceDatabase");
            Assert.ArgumentNotNull(context.PublishOptions.TargetDatabase, "context.PublishOptions.TargetDatabase");
            Assert.ArgumentCondition(!ID.IsNullOrEmpty(context.ItemId), "context.ItemId", "context.ItemId must be set!");
            Assert.ArgumentNotNull(context.User, "context.User");

            Database sourceDatabase = context.PublishOptions.SourceDatabase;
            Database targetDatabase = context.PublishOptions.TargetDatabase;
            ID itemId = context.ItemId;
            string userName = context.User.Name;
            Item item = context.PublishHelper.GetItemToPublish(context.ItemId);

            //Get your own custom log, for more details on how do make a custom log
            //go to http://firebreaksice.com/write-to-a-custom-sitecore-log-with-log4net/
            var logger = log4net.LogManager.GetLogger("Sitecore.Diagnostics.MyCustomLogger");
            logger.Info("Publishing Item :" + item.Name + " : " + item.ID.ToString() + " By User : " + userName);
            //Add the rest of the details to your log here
        }


    }
}

然后将此添加到您的包含配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <publishItem>
        <processor type="Sitecore.OpenSource.Pipelines.Publishing.LogPublicationInfoToCustomLog, ASSEMBLY_NAME"
                    patch:after="processor[@type='Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics, Sitecore.Kernel']" />
      </publishItem>
    </pipelines>
  </sitecore>
</configuration>