May 26, 2011

Add a custom property to the site node

Here is an easy way to add and retrieve a custom property to you site in the .config file.


Here is an example with my custom property SiteUID
  1. Add this property to your site tag:
    <site patch:after="*[contains(@name, 'login')]"
        hostName="uk.xxx"
        targetHostName="uk.xxx"
        name="UK"
        siteUID="MyUniqueID"
        virtualFolder="/"
        physicalFolder="/"
        rootPath="/sitecore/content/United Kingdom/Website"
        startItem="/Homepage"
        contentStartItem="/sitecore/content/United Kingdom/Website/Homepage"
        database="web"
        domain="extranet"
        allowDebug="true"
        cacheHtml="true"
        htmlCacheSize="10MB"
        registryCacheSize="0"
        viewStateCacheSize="0"
        xslCacheSize="5MB"
        filteredItemsCacheSize="2MB"
        enablePreview="true"
        enableWebEdit="true"
        enableDebugger="true"
        disableClientData="false"
        language="en"/>
  2. Create an extension method:
    /// <summary>
    /// Retrun the site unique ID
    /// </summary>
    /// <returns></returns>
    public static string SiteUID(this SiteContext site)
    {
        Assert.IsNotNull(site, "Site cannot be null");
                
        try 
        {         
            string uid = site.Properties["siteUID"];
            if (!String.IsNullOrEmpty(uid))
                return uid;
            else
                return site.Name;
        }
        catch (Exception)
        {
            return site.Name;
        }
    }
  3. Use this property in your code: (don't forget to add you using to the class with your extention method)
    Sitecore.Context.Site.SiteUID()

Write you messages in a separated file

It is not always easy with a sitecore "out of the box" to know witch messages come from your custom code and witch messages come from sitecore himself.
Here is a small tip to write you custom messages in a separated file:

First of all, we will prefix all our messages with a custom prefix "SH - " in my case:
Log.Info("SH - My message", typeof(MyObject));
Log.Error("SH - My message", typeof(MyObject));
...

And then to write it in a custom log file change in the web.config in the log4net section:
<appender name="MyFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
    <file value="$(dataFolder)/logs/mylog.{date}.txt" />
 <filter type="log4net.Filter.StringMatchFilter">
    <regexToMatch value="^SH .*" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
    </layout>
</appender>
You will need to adapt the regex with your custom prefix of course.

And second point after the logFileAppender of sitecore add you custom one:
<root>
    <priority value="DEBUG"/>
    <appender-ref ref="LogFileAppender"/>
    <appender-ref ref="MyFileAppender" />
</root>

You have a lot of example of what you can do with log4net here: http://logging.apache.org/log4net/release/config-examples.html

EDIT: You have an alternative syntaxt and more info about the filters here https://sitecoreblog.blogspot.be/2017/03/log4net-add-stacktrace-in-logs.html