I'm not sure if this is a bug (try getting in touch with support@kentico.com) but this workaround works:
protected void Custom_Code_Click(object sender, EventArgs e)
{
// Logs activity for current contact
if (ContactManagementContext.GetCurrentContact() != null)
{
// Obtains the activity logging service
var service = Service.Entry<IActivityLogService>();
// Prepares an initializer for logging the activity
var activityInitializer = new MyActivityInitializer("current contact");
// Logs the activity
service.Log(activityInitializer, CMSHttpContext.Current.Request);
}
// Logging the activity for a different contact
ContactInfo contact = ContactInfoProvider.GetContacts().Where("ContactEmail = 'bp@example.com'").TopN(1);
if (contact != null)
{
// set contact cookie to the logged contact
HttpCookie currentContactCookie = CookieHelper.GetExistingCookie("CurrentContact");
string tempCurrentContactCookieValue = currentContactCookie.Value;
CookieHelper.SetValue(currentContactCookie.Name, contact.ContactGUID.ToString(), currentContactCookie.Expires);
// Obtains the activity logging service
var service = Service.Entry<IActivityLogService>();
// Prepares an initializer for logging the activity
var activityInitializer = new MyActivityInitializer("different conntact");
// Logs the activity
service.Log(activityInitializer, CMSHttpContext.Current.Request);
// restore cookie value
CookieHelper.SetValue(currentContactCookie.Name, tempCurrentContactCookieValue, currentContactCookie.Expires);
}
}