Wednesday, December 22, 2010

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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...