This page contains information on creating and assigning a logger of an index, as well as on the implementation of a custom logger.
Use of standard file logger
In order to save information about events and errors in an index into a file, you should use the standard file logger or your own custom logger. It is important to remember that each time you open an existing index, you must create and assign an instance of the logger again, because the logger is not saved. The logger receives, among other things, the messages that the index reports while it is being opened, for example, about an interrupted indexing operation (see Checking index integrity). The same code can be used to create a new index and to open an existing one: when an existing index is opened, the logger is taken from the index settings passed to the constructor, and the other index settings are loaded from the index.
The following example demonstrates the use of the standard file logger. The constructor parameters of the FileLogger class are the path to the log file and its maximum size in MB. The maximum size should be in the range from 0.1 to 1000 MB; the value is not checked. When the log file exceeds the maximum size, the older part of the log is removed, and the most recent half of the maximum size is kept.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";stringquery="Einstein";stringlogPath=@"c:\Log.txt";IndexSettingssettings=newIndexSettings();settings.Logger=newFileLogger(logPath,4.0);// Specifying the path to the log file and a maximum length of 4 MBIndexindex=newIndex(indexFolder,settings);// Creating or loading an index from the specified folderindex.Add(documentsFolder);// Indexing documents from the specified folderSearchResultresult=index.Search(query);// Search in index
Implementing custom logger
Sometimes you may need to implement your own logger, for example, to display the log in the console. A custom logger implements the ILogger interface: the Error method logs errors, and the Trace method logs events. An example implementation of such a logger is presented below.
The custom logger is assigned in the same way as the standard one.
C#
stringindexFolder=@"c:\MyIndex\";IndexSettingssettings=newIndexSettings();settings.Logger=newConsoleLogger();// Assigning the custom loggerIndexindex=newIndex(indexFolder,settings);// Creating or loading an index from the specified folder
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: