Log Data Source Script (C#)

In this article:
We recommend reading Scripting in Application (C#) 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 fileName = Context.Options.FileName;
if (string.IsNullOrEmpty(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:
if (Context.Data.TryGetValue("reader", out TextReader reader))
    reader.Dispose();
⬆️ Back to top

Namespaces

The following namespaces are included by default:
  • System.IO
  • CarinaStudio.ULogViewer.Logs.DataSourcesDeprecated 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(string optionName): Boolean
      Check whether specific option was set with value or not.
    • IsResourceOnAzure: Boolean
    • Password: String?
    • ProcessId: Int32?v4.0+
    • ProcessName: String?v4.0+
    • QueryString: String?
    • SetupCommands: IList<String>
    • TeardownCommands: IList<String>
    • UserName: String?
    • Uri: System.Uri?
    • WorkingDirectory: String?
⬆️ Back to top