Log Errors to Windows Event Log or SharePoint Log Programmatically
Using Windows Event – Logs
During development its worth to log errors either in Windows Event log or in SharePoint log files for further troubleshooting.
try
{
//Do something error-prone
}
catch (Exception ex)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
System.Diagnostics.EventLog.WriteEntry("My Custom Solution", ex.Message + " Inner Exception: " + ex.InnerException + " Call Stack: " + ex.StackTrace, EventLogEntryType.Error);
});
}
Beside this you can also use Trace.write or Debug.write
Use SharePoint Log Files:
try
{
//Do something error-prone action
}
catch (Exception ex)
{
//In SharePoint 2010
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Custom Solution", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
//In MOSS 2007
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occurred: {0} || {1}", ex.Message, ex.StackTrace);
}