承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

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();
       }

    }
}

posted on 2011-01-06 21:59  A A  阅读(372)  评论(0编辑  收藏  举报