Log Data Source Script (JavaScript)

In this article:
We recommend reading Scripting in Application (JavaScript) first to learn the basics of scripting.

Samples

The following is sample script to use user-provided pattern (regular expression) to read and filter raw log from file:
⬆️ Back to top

Sub Scripts

There are 3 sub scripts in each log data source script:

Script to Open Raw Log Reader (Required)

The script will be invoked one time when trying to open a reader for reading raw log lines. The type of returned value is LogDataSourceState. Return LogDataSourceState.readerOpened if the reader is opened successfully, or return the following values for failure cases:
  • LogDataSourceState.sourceNotFound
    The source of raw log cannot be found.
  • LogDataSourceState.externalDependencyNotFound
    One or more external dependencies were not found.
  • LogDataSourceState.unclassifiedError
    An unclassified error occurred.
Example:
var StreamReader = importNamespace("System.IO").StreamReader;
var Encoding = importNamespace("System.Text").Encoding;
 
var fileName = context.options.fileName;
if (fileName == null || fileName == "")
{
    context.logger.logError("No file name specified");
    return LogDataSourceState.unclassifiedError;
}
 
context.data["reader"] = new StreamReader(fileName, Encoding.utf8);
return LogDataSourceState.readerOpened;

Script to Read Raw Log Line (Required)

The script will be invoked every time when trying to read raw log line. The returned value should be a raw log line (String), or Null if there are no more raw log lines to read.
Example:
return context.data["reader"].readLine();

Script to Close Raw Log Reader (Optional)

The script will be invoked one time when closing reader for reading raw log lines. There is no returned value needed.
Example:
var reader = context.data["reader"];
if (reader != null)
    reader.dispose();
⬆️ Back to top

Namespaces

The following namespaces are included by default:
  • System.IO as sysIO.
  • CarinaStudio.ULogViewer.Logs.DataSources as csUlvDataSources.Deprecated in v3.0+
    To access types and functions of log data source.
CarinaStudio.* namespaces should not be imported manually starting from ULogViewer 3.0. All necessary types will be imported automatically.
⬆️ Back to top

Context

The following are members of Context other than basic members:
  • options: LogDataSourceOptions
    Data source options for opening reader and reading raw log lines. The following are members of LogDataSourceOptions:
    • category: string
    • command: string
    • connectionString: string
    • encoding: System.Text.Encoding
    • environmentVariables: IDictionary<String, String>v4.0+
    • fileName: string
    • formatJsonData: boolean
    • formatXmlData: boolean
    • includeStandardError: boolean
    • ipEndPoint: System.Net.IPEndPoint
    • isOptionSet(optionName: string): boolean
      Check whether specific option was set with value or not.
    • isResourceOnAzure: boolean
    • password: string
    • processId: System.Nullable<int>v4.0+
    • processName: stringv4.0+
    • queryString: string
    • setupCommands: Array
    • teardownCommands: Array
    • userName: string
    • uri: System.Uri
    • workingDirectory: string
⬆️ Back to top