SharePoint Write Logs
using System;
using System.Security.Permissions;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace AnnouncementListEvent.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being added.
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
writeDataToLogFile(properties, "ItemAdding Event");
writeListItemToCustomList(properties, "ItemAdding Event");
}
private void writeListItemToCustomList(SPItemEventProperties properties, string eventName)
{
string spLog = "";
DateTime currentTime = DateTime.Now;
spLog = eventName + " " + currentTime.ToString();
using (SPSite site = new SPSite("http://moss2010/"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Log"];
SPListItem Item = list.Items.Add();
Item["Title"] = properties.ListTitle.ToString();
Item["Log Entry"] = spLog;
Item.Update();
}
}
}
private void writeDataToLogFile(SPItemEventProperties properties, string eventName)
{
FileIOPermission myPermissions = new FileIOPermission(PermissionState.Unrestricted);
myPermissions.AddPathList(FileIOPermissionAccess.AllAccess, "c:\\Authoring");
StreamWriter sw = File.AppendText(@"C:\Authoring\mySPLog.txt");
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Date, Event and List:\n {0} {1} {2} ", DateTime.Now.ToString(), eventName, properties.ListTitle);
sw.WriteLine(sb.ToString());
sw.Close();
}
}
}