Sample Method call:
LogMessage(string.Format("SP group '{0}' does not have enough permission.", NewItServiceAdminGroupName), EventLogEntryType.Error, true, "Feature Activation", EventSeverity.ErrorCritical, TraceSeverity.Unexpected);
Method used:
/// <summary> /// Log message to event log and SharePoint log. /// </summary> /// <param name="message">Message to log.</param> /// <param name="eventLogEntryType">Specifies the type of event entry log.</param> /// <param name="logInSharePointLog">Flag to include SharePoint log entry as well.</param> /// <param name="eventCategoryName">SharePoint event category name.</param> /// <param name="eventSeverity">SharePoint event severity.</param> /// <param name="traceSeverity">SharePoint trace severity.</param> public static void LogMessage(string message, EventLogEntryType eventLogEntryType, bool logInSharePointLog, string eventCategoryName, EventSeverity eventSeverity, TraceSeverity traceSeverity) { SPSecurity.RunWithElevatedPrivileges(delegate() { const string sourceName = "SharePoint Custom Solutions"; const string logName = "Application"; if (!EventLog.SourceExists(sourceName)) { EventLog.CreateEventSource(sourceName, logName); } // Writing to -> Windows Logs/Application EventLog.WriteEntry(sourceName, message, eventLogEntryType, 1001, 100); if (logInSharePointLog) { SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local; SPDiagnosticsCategory categoryOfTheTrace = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"]; SPDiagnosticsService.Local.WriteTrace(1, categoryOfTheTrace, traceSeverity, message, categoryOfTheTrace.Name, categoryOfTheTrace.Area.Name); } }); }
Windows Server Event Viewer Output:
SharePoint ULS Viewer Output:
Possible values for TraceSeverity & EventSeverity:
EventSeverity