Beginners will use TXT text writing to achieve log saving, but text writing is not thread safe, when there are multiple threads writing to the log, there will be some problems. Log4net library is. The next great open source logging component in.NET is a tool that helps developers export logging information to a variety of targets (consoles, files, databases, etc.). In this section, the open-source component Log4Net is used to store the text of ERROR information. In combination with SQLite database, log INFO information is stored in the database for subsequent query.
SQLite store
The advantage of storing log information in the database is that it is easy for users to query logs through the interface. The open source free database SQLite is used here. Other relational databases, such as SQLServer and mysql, have the same principle, and the specific steps are as follows:
Create databases and tables
Use SQLiteStudio software to create a database, named Log4NetSQLite, execute the following script to create a Log table:
BufferSize: When the number of log cache writes is set to 0, write to the database immediately as long as there is one
ConnectionString: SQLite points to the absolute path of the database file
Add an Info method to the LogHelper class like this: