In this post I describe how to write information to the App Error Log.
Microsoft published an article about that: Adding troubleshooting instrumentation to an app for SharePoint. While figuring out the product GUID I found out that logging to the app log is easier that described in the article.
The following snipped describes in short what you have to do. There is no need to figure out the Product GUID.
var m_appContext = {...}; // Assign your context class instance here.
try {
throw new Error("This is an error.");
}
catch (error) {
SP.Utilities.Utility.logCustomAppError(m_appContext, error);
m_appContext.executeQueryAsync(
function () { },
function () { alert("Could not write to error log.") });
}
SharePoint dynamically generates a JavaScript “sp.debug.js” where the logCustomAppError method is encapsulated referencing the product GUID. You can check this out if you set a breakpoint in your project and step into the function call.
new SP.ClientActionInvokeStaticMethod(context, '{16f43e7e-bf35-475d-b677-9dc61e549339}', 'LogCustomAppError', [error]);
It takes a couple of minutes untill the entries apprear in the log. You can navigate to the log from your host web. First navigate to “Site Contents”.
Then klick on the dots “…” of the app entry.
Now you can click on “Details” to navigate to the App Details page where you can see the Errors.
In my opinion, every app should make use of this.

January 7, 2013 



One Response to “Add logging to your SharePoint App”