📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

NativeScript Core

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.

const traceModule = require("tns-core-modules/trace");
import { enable, disable, isEnabled, categories, setCategories, addCategories, write, isCategorySet } from "tns-core-modules/trace";

Custom Trace Writer

The example shows how to create custom writer and to attached it to the tracing categories.

Create custom writer

const array = new ObservableArray();
const TimestampConsoleWriter = (() => {

    function TimestampConsoleWriter() { }
    TimestampConsoleWriter.prototype.write = function (message, category, type) {
      if (!console) {
        return;
      }

      const msgType = types.isUndefined(type) ? traceModule.messageType.log : type;

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

    return TimestampConsoleWriter;
  })();
const array = new ObservableArray();
class TimestampConsoleWriter {
    constructor() { }

    public write(message, category, type) {
        if (!console) {
            return;
        }

        const msgType = isUndefined(type) ? messageType.log : type;

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

Adding custom trace writer

traceModule.setCategories(traceModule.categories.Navigation);
traceModule.enable();
traceModule.clearWriters();
traceModule.addWriter(new TimestampConsoleWriter());
setCategories(categories.Navigation);
enable();
clearWriters();
addWriter(new TimestampConsoleWriter());

Improve this document

Demo Source


Trace Specific Categories

In this example is demonstrated, how to trace a specific set of event categories, how to add new tracing category and to disable the tracing process at all.

Tracing specific categories of events

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

Trace add category

traceModule.addCategories(traceModule.categories.Navigation);
addCategories(categories.Navigation);

Check is category setting

if (traceModule.isCategorySet(traceModule.categories.VisualTreeEvents)) {
    dialogs.alert("VisualTreeEvents category has been set")
    .then(() => {
        console.log("Dialog closed!");
    });
} else {
    dialogs.alert("VisualTreeEvents category has not been set")
    .then(() => {
        console.log("Dialog closed!");
    });
}
if (isCategorySet(categories.VisualTreeEvents)) {
    dialogs.alert("VisualTreeEvents category has been set")
        .then(() => {
            console.log("Dialog closed!");
        });
} else {
    dialogs.alert("VisualTreeEvents category has not been set")
        .then(() => {
            console.log("Dialog closed!");
        });
}

Disable tracing

traceModule.disable();
disable();

Improve this document

Demo Source


API Reference for the Trace Class