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.
- Writing the exceptions in the screen or throw the exception when raised
- Write the exception as a comment using the literal.
- 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.
Steps
- Download the
Rjesh.Cloud.Exception.Logging
WSP - sandbox solution from the below provided link - Add the wsp to the site collection solutions
- Activate the solution
- Go to the site features and activate the feature
<Rjesh.Cloud.Exception.Logging>
- Download the Logger.cs file from the below provided link
- Add the cs file to your solution where you want to use logging
- Add the
namespace
in your code - 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
try
{
}
catch(Exception ex)
{
Logger.LogException(siteUrl, ex, Logger.ExceptionSeverity.CriticalError);
}
You can download Logger.cs, Rjesh.Cloud.Exception.Logging.wsp here.