in shared/AppInsightsCore/src/JavaScriptSDK/DiagnosticLogger.ts [92:243]
constructor(config?: IConfiguration) {
/**
* Count of internal messages sent
*/
let _messageCount = 0;
/**
* Holds information about what message types were already logged to console or sent to server.
*/
let _messageLogged: { [msg: number]: boolean } = {};
dynamicProto(DiagnosticLogger, this, (_self) => {
if (isNullOrUndefined(config)) {
config = {};
}
_self.consoleLoggingLevel = () => _getConfigValue("loggingLevelConsole", 0);
_self.telemetryLoggingLevel = () => _getConfigValue("loggingLevelTelemetry", 1);
_self.maxInternalMessageLimit = () => _getConfigValue("maxMessageLimit", 25);
_self.enableDebugExceptions = () => _getConfigValue("enableDebugExceptions", false);
/**
* This method will throw exceptions in debug mode or attempt to log the error as a console warning.
* @param severity {LoggingSeverity} - The severity of the log message
* @param message {_InternalLogMessage} - The log message.
*/
_self.throwInternal = (severity: LoggingSeverity, msgId: _InternalMessageId, msg: string, properties?: Object, isUserAct = false) => {
const message = new _InternalLogMessage(msgId, msg, isUserAct, properties);
if (_self.enableDebugExceptions()) {
throw message;
} else {
// Get the logging function and fallback to warnToConsole of for some reason errorToConsole doesn't exist
let logFunc = severity === LoggingSeverity.CRITICAL ? strErrorToConsole : strWarnToConsole;
if (!isUndefined(message.message)) {
const logLevel = _self.consoleLoggingLevel();
if (isUserAct) {
// check if this message type was already logged to console for this page view and if so, don't log it again
const messageKey: number = +message.messageId;
if (!_messageLogged[messageKey] && logLevel >= severity) {
_self[logFunc](message.message);
_messageLogged[messageKey] = true;
}
} else {
// Only log traces if the console Logging Level is >= the throwInternal severity level
if (logLevel >= severity) {
_self[logFunc](message.message);
}
}
_self.logInternalMessage(severity, message);
} else {
_debugExtMsg("throw" + (severity === LoggingSeverity.CRITICAL ? "Critical" : "Warning"), message);
}
}
}
/**
* This will write a warning to the console if possible
* @param message {string} - The warning message
*/
_self.warnToConsole = (message: string) => {
_logToConsole("warn", message);
_debugExtMsg("warning", message);
}
/**
* This will write an error to the console if possible
* @param message {string} - The error message
*/
_self.errorToConsole = (message: string) => {
_logToConsole("error", message);
_debugExtMsg("error", message);
}
/**
* Resets the internal message count
*/
_self.resetInternalMessageCount = (): void => {
_messageCount = 0;
_messageLogged = {};
};
/**
* Logs a message to the internal queue.
* @param severity {LoggingSeverity} - The severity of the log message
* @param message {_InternalLogMessage} - The message to log.
*/
_self.logInternalMessage = (severity: LoggingSeverity, message: _InternalLogMessage): void => {
if (_areInternalMessagesThrottled()) {
return;
}
// check if this message type was already logged for this session and if so, don't log it again
let logMessage = true;
const messageKey = AIInternalMessagePrefix + message.messageId;
// if the session storage is not available, limit to only one message type per page view
if (_messageLogged[messageKey]) {
logMessage = false;
} else {
_messageLogged[messageKey] = true;
}
if (logMessage) {
// Push the event in the internal queue
if (severity <= _self.telemetryLoggingLevel()) {
_self.queue.push(message);
_messageCount++;
_debugExtMsg((severity === LoggingSeverity.CRITICAL ? "error" : "warn"), message);
}
// When throttle limit reached, send a special event
if (_messageCount === _self.maxInternalMessageLimit()) {
const throttleLimitMessage = "Internal events throttle limit per PageView reached for this app.";
const throttleMessage = new _InternalLogMessage(_InternalMessageId.MessageLimitPerPVExceeded, throttleLimitMessage, false);
_self.queue.push(throttleMessage);
if (severity === LoggingSeverity.CRITICAL) {
_self.errorToConsole(throttleLimitMessage);
} else {
_self.warnToConsole(throttleLimitMessage);
}
}
}
};
function _getConfigValue<T>(name: keyof IConfiguration, defValue: T): T {
let value = config[name] as T;
if (!isNullOrUndefined(value)) {
return value;
}
return defValue;
}
function _areInternalMessagesThrottled(): boolean {
return _messageCount >= _self.maxInternalMessageLimit();
}
function _debugExtMsg(name: string, data: any) {
let dbgExt = getDebugExt(config);
if (dbgExt && dbgExt.diagLog) {
dbgExt.diagLog(name, data);
}
}
});
}