catch (e) {
logMessageToULS(e.message, 'Custom.js');
}
Error
Log File
//
Registering the ULS logging function on a global level.
window.onerror
= logErrorToULS;
//
Set default value for teamName.
var teamName = "Biraj
Office App";
//
Further add the logErrorToULS method at the end of the script.
function logMessageToULS(message, fileName) {
if (message != null) {
var ulsObj = new ulsObject();
ulsObj.message = message;
ulsObj.file = fileName;
ulsObj.line = 0; // We don't know the line, so we set it to zero.
ulsObj.stack =
getCallStack(logMessageToULS.caller);
//ulsObj.client
= getClientInfo();
ulsObj.team = teamName;
ulsObj.originalFile = ulsObj.file;
var soapPacket = generateErrorPacket(ulsObj);
postMessageToULSSvc(soapPacket);
}
}
//
Function to log messages to Diagnostic web service.
//
Invoked by the window.onerror message.
function logErrorToULS(msg, url, linenumber) {
var ulsObj = new ulsObject();
ulsObj.message = "Error occurred: " +
msg;
ulsObj.file =
url.substring(url.lastIndexOf("/") + 1); // Get the current
file name.
ulsObj.line = linenumber;
ulsObj.stack =
getCallStack(logErrorToULS.caller); //
Create error call stack.
ulsObj.team = teamName; // Declared in the consumer script.
ulsObj.originalFile = ulsObj.file;
var soapPacket = generateErrorPacket(ulsObj); // Create the soap packet.
postMessageToULSSvc(soapPacket); // Post to the web service.
return true;
}
//
Creates a custom ulslog object
//
with the required properties.
function ulsObject() {
this.message = null;
this.file = null;
this.line = null;
this.client = null;
this.stack = null;
this.team = null;
this.originalFile = null;
}
function getCallStack(functionDef, depth) {
if (functionDef != null) {
var signature = '';
functionDef = functionDef.toString();
signature = functionDef.substring(0,
functionDef.indexOf("{"));
if (signature.indexOf("function") == 0) {
signature = signature.substring(8);
}
if (depth == 0) {
var stack = "<stack><function
depth='0' signature='{0}'>{1}</function></stack>";
stack = String.format(stack,
signature, functionDef);
}
else {
var stack = "<stack><function
depth='1' signature='{0}'></function></stack>";
stack = String.format(stack,
signature);
}
return stack;
}
return "";
}
//
Creates the SOAP packet required by SendClientScriptErrorReport
//
web method.
function generateErrorPacket(ulsObj) {
var soapPacket = "<?xml
version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
" +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<SendClientScriptErrorReport " +
"xmlns=\"http://schemas.microsoft.com/sharepoint/diagnostics/\">" +
"<message>{0}</message>" +
"<file>{1}</file>" +
"<line>{2}</line>" +
"<stack>{3}</stack>" +
"<client>{4}</client>" +
"<team>{5}</team>" +
"<originalFile>{6}</originalFile>" +
"</SendClientScriptErrorReport>" +
"</soap:Body>" +
"</soap:Envelope>";
soapPacket = String.format(soapPacket,
encodeXmlString(ulsObj.message), encodeXmlString(ulsObj.file),
ulsObj.line,
encodeXmlString(ulsObj.stack), encodeXmlString(ulsObj.client),
encodeXmlString(ulsObj.team),
encodeXmlString(ulsObj.originalFile));
return soapPacket;
}
//
Utility function to encode special characters in XML.
function encodeXmlString(txt) {
txt = String(txt);
txt = jQuery.trim(txt);
txt = txt.replace(/&/g, "&");
txt = txt.replace(/</g, "<");
txt = txt.replace(/>/g, ">");
txt = txt.replace(/'/g, "'");
txt = txt.replace(/"/g, """);
return txt;
}
//
Function to form the Diagnostics service URL.
function getWebSvcUrl() {
var serverurl = location.href;
if (serverurl.indexOf("?") != -1) {
serverurl =
serverurl.replace(location.search, '');
}
var index = serverurl.lastIndexOf("/");
serverurl = serverurl.substring(0, index -
1);
serverurl = serverurl.concat('/_vti_bin/diagnostics.asmx');
return serverurl;
}
//
Method to post the SOAP packet to the Diagnostic web service.
function postMessageToULSSvc(soapPacket) {
$(document).ready(function () {
$.ajax({
url: getWebSvcUrl(),
type: "POST",
dataType: "xml",
data: soapPacket, //soap packet.
contentType: "text/xml; charset=\"utf-8\"",
success: handleResponse, // Invoke when the web service call is successful.
error: handleError// Invoke when the web service call fails.
});
});
}
//
Invoked when the web service call succeeds.
function handleResponse(data, textStatus, jqXHR) {
//
Custom code...
$('#message').html('Something went
wrong. Please try after sometime!');
}
//
Invoked when the web service call fails.
function handleError(jqXHR, textStatus, errorThrown) {
//Custom
code...
$('#message').html('Error occurred in
executing the web request');
}