Using the Sitecore Audit Log

I guess your code contains just as many log messages as my code does:

try
{
  // lots of stuff going on here...
}
catch (Exception ex)
{
  Sitecore.Diagnostics.Log.Error("Some great error message: " + ex.Message, this);
  // more exception handling here...
}

And the exception is written to the log:

6636 13:35:42 ERROR Some great error message: blahblahblah

In previous versions, the Audit log was a seperate log. But now it’s all handled by Apache Log4Net. So writing to the audit log is similar to writing to the log. The following line:

Sitecore.Diagnostics.Log.Audit("DamForSitecore:RemoveAssetFromCache. AssetID=" + assetID, this);

Produces the following log line:

3360 13:56:27 INFO  AUDIT (sitecore\admin): DamForSitecore:RemoveAssetFromCache. AssetID=4848

You can use the AuditFormatter to format items and fields so they appear with the same pattern for all items and fields in the log.
The following line:

Sitecore.Diagnostics.Log.Audit(this, "Do stuff {0}", new[] {AuditFormatter.FormatItem(Sitecore.Context.Item)});

Produces the following log line:

3360 14:06:18 INFO  AUDIT (sitecore\admin): Do stuff: master:/sitecore/content/Home, language: da, version: 2, id: {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}

Other articles regarding the subject:

 

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6 and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s