Log Data Source Script (C#)
In this article:
Suggest reading Scripting in Application (C#) first to get the basis 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 topSub 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 opening reader for reading raw log lines. The type of returned value is LogDataSourceState. Returning LogDataSourceState.ReaderOpened if reader opened successfully, Or returning 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
Unclassified error was occurred.
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;
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 raw log line (String), or Null if there is no more raw log line can be 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
reader.Dispose();
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 followings are members of Context other than basic members:-
Options: LogDataSourceOptions
Data source options for opening reader and reading raw log lines. The followings 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?