SharePoint 2010 Write Log file
Below Code will log the Action name in List and text file
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;
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”);
}
{
/// <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();
{
string spLog = “”;
DateTime currentTime = DateTime.Now;
spLog = eventName + ” ” + currentTime.ToString();
using (SPSite site = new SPSite(“http://SERVER:8888/”))
{
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();
}
}
{
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”);
{
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();
}
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();
}
}
}
}