Save log4net to database series 1: Configure log4net and webconfiglog4net in WebConfig
There are many posts in the garden about how to save log4net to the database, but it is still not easy to do it. Learn the log4net database logs from the beginning.
- 1. Configure log4net in WebConfig
- 2. Configure log4net in the independent configuration file
- 3. Configure log4net in xml Code
- Iv. complete code configuration log4net
- 5. Add database fields
Next, we will learn how to configure it.
1. Download and install
- Nuget console installation:
Install-Package log4net
- Download the latest official version:
Http://logging.apache.org/log4net/download_log4net.cgi
2. Create a database script
1 create table [dbo]. [Log] (2 [Id] [int] IDENTITY (1, 1) not null, 3 [Date] [datetime] not null, 4 [Thread] [varchar] (255) not null, 5 [Level] [varchar] (50) not null, 6 [Logger] [varchar] (255) not null, 7 [Message] [varchar] (4000) not null, 8 [Exception] [varchar] (2000) nula9)Database script
3. Configuration File
Added configSections nodes for WebConfig.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<Log4net> <! -- Register Appender --> <root> <level value = "ALL"/> <appender-ref = "AdoNetAppender"/> </root> <! -- Configure Appender --> <appender name = "AdoNetAppender" type = "log4net. appender. adoNetAppender "> <bufferSize value ="-1 "/> <connectionType value =" System. data. sqlClient. sqlConnection, System. data, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "/> <connectionString value =" data source = [server address]; initial catalog = [database]; persist security info = True; user id = [account]; password = [password]; MultipleActiveResultSets = True; "/> <commandText value =" insert into Log ([Date], [Thread], [Level], [Logger], [Message], [Exception]) VALUES (@ log_date, @ thread, @ log_level, @ logger, @ message, @ exception) "/> <parameter> <parameterName value =" @ log_date "/> <dbType value =" DateTime "/> <layout type =" log4net. layout. rawTimeStampLayout "/> </parameter> <parameterName value =" @ thread "/> <dbType value =" String "/> <size value =" 255 "/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % thread "/> </layout> </parameter> <parameterName value =" @ log_level "/> <dbType value =" String "/> <size value =" 50 "/> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" % level "/> </layout> </parameter> <parameterName value =" @ logger "/> <dbType value =" String & quot;/> <size value = "255"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % logger "/> </layout> </parameter> <parameterName value =" @ message "/> <dbType value =" String & quot;/> <size value = "4000"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % message "/> </layout> </parameter> <parameterName value =" @ exception "/> <dbType value =" String & quot;/> <size value = "2000"/> <layout type = "log4net. layout. predictionlayout "/> </parameter> </appender> </log4net>Configure the log4net node in WebConfig
4. Set monitoring
Add code in AssemblyInfo. cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
5. Test code
ILog log = log4net. logManager. getLogger (MethodBase. getCurrentMethod (). declaringType); log. error ("Error is an Error", new Exception ("Error"); log. fatal ("Fatal is a serious error", new Exception ("Fatal error"); log. info ("Info is a common message"); log. debug ("Debug is a debugging message ");View Code
6. The test is successful.