Friday, June 11, 2010

Logging into EventLog in SharePoint Custom Code

While writing custom development code in SharePoint, we surely need some kind of logging mechanism other than the one SharePoint provides out of the box in 12 Hive. The following sample function can be called at anytime ( in web part, timer jobs etc)


public static void LogMessage(string message)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        if (!EventLog.SourceExists("SharePoint Custom Solutions"))
        {
            EventLog.CreateEventSource("SharePoint Custom Solutions", "Application");
        }

        EventLog.WriteEntry("SharePoint Custom Solutions", message, EventLogEntryType.Error);
    });
}

Technorati Tags: ,,,

No comments:

Post a Comment