1、 introduction
In the development of upper computer , Logging is essential , We can do log analysis and error tracking through logging . Beginners will use txt Write text to realize log saving , But text writing is not thread safe , When there are multiple threads writing to the log at the same time , There will be some problems .Log4net Kuo is .Net The next excellent open source logging component , It is a tool to help developers output log information to various targets ( Console 、 file 、 Database etc. ) Tools for . This section mainly adopts open source components Log4Net To implement the error ERROR Information text storage , And combine SQLite database , Log INFO Information is stored in the database , It is convenient for subsequent query .
2、 The development of preparation
- First, create a new one Windows Form application , named thinger.cn.Log4NetSQLitePro,UI The interface design is as follows :
- adopt Nuget add to Log4Net Open source components , As shown below :
- adopt Nuget add to SQLite Components , As shown below :
3、 The text is stored
In general , We can store some error and exception information in text , It is convenient to open the file for query at any time , The text store will automatically be in days , One file per day , Steps are as follows :
Add an application configuration file
Right click item to add new item , Project type selection application profile , The name is log4net.config, As shown below :
Configuration file writing
Add related nodes to the log configuration file , As shown below :
The configuration file specifies the relevant attributes of log information 、 storage 、 Log content format, etc , The configuration information is shown below :
among , More importantly, the format of log information , Corresponding to the... In the above file ConversionPattern, The value is "[%d]%n%m%n%n", Each placeholder has a corresponding meaning , As shown below :
- Copy from the profile properties to the output directory , Set to always copy or copy if newer , As shown in the figure below :
Project AssemblyInfo.cs Class , As shown below :
Add one LogHelper class , To write 2 individual Error Methods , As shown below :
stay ini Text store button event , Call error log write , As shown below :
After execution , In the project directory ,Log\Error Under the table of contents , Generate a file named by the log of the day , Open as shown below
4、SQLite Storage
The advantage of storing log information in the database is that it is convenient for users to query through the interface , Open source free database is adopted here SQLite, Other relational databases , Such as SQLServer、mysql, The principle is the same , The specific steps are as follows :
Create databases and tables
adopt SQLiteStudio The software creates a database , named Log4NetSQLite, Execute the following script to create a Log Data sheet :
Copy the database file to... Under the root directory of the project DataBase In the folder
modify log4net.config file , Add database storage related configuration , As shown below :
bufferSize: Number of log cache writes Set to 0 When there is one, it will be written to the database immediately
connectionString:SQLite It points to the absolute path of the database file
LogHelper Class Info Method , As shown below :
stay SQLite Store button events , call Info Log write , As shown below :
After execution , Open database , Check whether there are relevant records :
5、 The practical application
Through the above series of descriptions , I'm sure you're right Log4Net Have some understanding of the application of ,Log4Net The built log system is a necessary function of many projects , Project development 、 Debugging and subsequent maintenance are very important . In actual use , We can also put Log4Ne As a simple data storage tool , You can even use Log4Net Do multi table and multi database storage .