Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Exception Logging in Office 365 - SharePoint Online Custom Solutions

0.00/5 (No votes)
9 Jul 2012CPOL3 min read 16K  
Exception logging in Office 365 - SharePoint online custom solutions

Normally, we use various logging options in SharePoint custom development, like writing to event log, ULS log, writing to developer dashboard, some use enterprise library logging block to write log to database as well. We don't have this freedom when developing sandbox solutions. Even in this case, we can create full trust proxy to write logs to ULS and try to consume that in our sandbox solution. This works fine for the sandbox solutions which are deployed to the on premise environments, but in case of SharePoint online, i.e., Office 365, we don't have privileges to view the ULS logs.

How can we write the exception messages which can help us in debugging or troubleshooting the issue. I'm trying to list down few available options which came in my mind.

I won't favor this option. As part of good user experience, we won't expect our end users to look into the exception message or nasty stack trace messages.

When the exception is written as comment block, it won't be visible to the naked eyes of the end user, developer can see the source code once the page is rendered in the browser. Use some specific keywords so the exception can be searched easily. The drawback with this option is it's not possible to write to the literal as comment all the time. Like in scenario like writing event receiver or workflow, you can't write everything to the UI.

With this option, you can write the exception to a custom list when occurred. I'm in favor of this option. You can refer to custom list at any point of time and fully leverage the Sharepoint capabilities. Like configuring alerts, workflows which can notify admin when any exception occurred.

  1. Writing the exceptions in the screen or throw the exception when raised
  2. Write the exception as a comment using the literal.
  3. Write the exception to custom list.

I have tried to create a reusable exception logging for Office 365 Sandbox custom solutions, I have used the above said option.

How It Works

Whenever any exception occurs in our custom solution, it can be written to the list in the SharePoint site itself. So I have created a content type which can cater to different fields associated with the logs. A list instance will be created which will have the above content type associated. Use the Logger class method which has methods to write to the logs. Whenever there is a need for any logging in the custom solution, make sure the feature is activated which will create list instance and use logger class methods and start logging.

Image 1

Steps

  1. Download the Rjesh.Cloud.Exception.Logging WSP - sandbox solution from the below provided link
  2. Add the wsp to the site collection solutions
  3. Activate the solution
  4. Go to the site features and activate the feature <Rjesh.Cloud.Exception.Logging>
  5. Download the Logger.cs file from the below provided link
  6. Add the cs file to your solution where you want to use logging
  7. Add the namespace in your code
  8. Use the logging methods as described below in the snippet, you can use the overload methods based on your logging needs.

How to Use the Logger

C#
try
{
//your code
}
catch(Exception ex)
{
//Change the exception severity accordingly.
 Logger.LogException(siteUrl, ex, Logger.ExceptionSeverity.CriticalError);
}

You can download Logger.cs, Rjesh.Cloud.Exception.Logging.wsp here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)