NativeScript Angular

Trace

Tracing is the process of logging diagnostic information about your application at runtime. This module is useful for debugging, which could provide detailed info about internal workings.

Custom Trace Writer

Import Trace Module and Utils Types

import {
    setCategories,
    enable,
    categories,
    messageType,
    clearWriters,
    addWriter,
    disable
} from "tns-core-modules/trace";
import { isUndefined } from "tns-core-modules/utils/types";

Create custom writer

class TimestampConsoleWriter {
    public array = [];

    public write(message, category, type) {
        if (!console) {
            return;
        }
        let msgType = isUndefined(type) ? messageType.log : type;

        switch (msgType) {
            case messageType.log:
                this.array.push({
                    "messageType": "log",
                    "date": new Date().toISOString(),
                    "message": message,
                    "category": category
                });
                break;
            case messageType.info:
                this.array.push({
                    "messageType": "info",
                    "date": new Date().toISOString(),
                    "message": message,
                    "category": category
                });
                break;
            case messageType.warn:
                this.array.push({
                    "messageType": "warning",
                    "date": new Date().toISOString(),
                    "message": message,
                    "category": category
                });
                break;
            case messageType.error:
                this.array.push({
                    "messageType": "error",
                    "date": new Date().toISOString(),
                    "message": message,
                    "category": category
                });
                break;
            default:
                break;
        }
    }
}

Adding custom trace writer

setCategories(categories.Navigation);
enable();
this.customwriter = new TimestampConsoleWriter();
clearWriters();
addWriter(this.customwriter);

Improve this document

Demo Source


Trace Specific Categories

Import Trace Module

import {
    setCategories,
    enable,
    disable,
    categories,
    addCategories,
    isCategorySet,
    isEnabled,
    write,
    clearWriters
} from "tns-core-modules/trace";

Tracing specific categories of events

setCategories(categories.concat(
    categories.Binding,
    categories.Layout,
    categories.Style,
    categories.ViewHierarchy,
    categories.VisualTreeEvents
));
enable();

Trace add category

addCategories(categories.Navigation);

Check is category setting

if (isCategorySet(categories.VisualTreeEvents)) {
    alert("VisualTreeEvents category has been set");
} else {
    alert("VisualTreeEvents category has not been set");
}

Disable tracing

disable();

Improve this document

Demo Source


API Reference for the Trace Class