Write To Log

Write To Log

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Configuration.Settings.Install 

Write To Log C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

Sitecore.Security.Accounts.User user = Sitecore.Context.User;
if (user == null) return;

Sitecore.Security.UserProfile profile = user.Profile;
if (profile == null) return;

try
{

    Sitecore.Diagnostics.Log.Info(user.Name + " is logged in", this);
    if (string.IsNullOrEmpty(profile.GetCustomProperty("foo")))
        user.Profile.SetCustomProperty("foo", "bar");
    Sitecore.Diagnostics.Log.Warn(user.Name + " custom property 'foo' set to 'bar' value", this);
    profile.SetCustomProperty("foo", "bar2");
    
}
catch (System.Exception ex)
{
    
    Sitecore.Diagnostics.Log.Error(ex.Message, this);
}

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag(); //Div

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

if (!string.IsNullOrEmpty(profile.GetCustomProperty("foo")))
     output.Write("{0}&#58;&#160;{1}", "Foo Custom Property", "<b>" + profile.GetCustomProperty("foo") + "</b><br/>");

output.RenderEndTag(); //Div


Write To Log C# Web Control Output

Foo Custom Property: bar2


Notes

  • Add the following entry to <xslextensions> section of web.config <extension mode="on" type="Sitecore.Diagnostics.Log, Sitecore.Kernel" namespace=http://www.sitecore.net/log singleInstance="true" />
  • Assign prefix in xslt header for namespace defined above in web.config <xmlns:log=http://www.sitecore.net/log exclude-result-prefixes="dot sc log">
  • XSLT Code: <xsl:value-of select="log:Warn('Warning message Title', 'Warning message body')" />