[Original] ASP. NET Web development, printing Log, detailed steps, asp. netlog
Four steps are required to add a Log:
I. Reference log4net. dll, see the attachment: http://pan.baidu.com/s/1c0hab2g
2. Add log4net. xml under the project root directory
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 <configSections> 4 <section name = "log4net" type = "System. Configuration. IgnoreSectionHandler"/> 5 </configSections> 6 <log4net> 7 <! -- Define output to file --> 8 <appender name = "LogFileAppender" type = "log4net. Appender. FileAppender"> 9 <! -- Define the file storage location --> 10 <file value = "D:/log4netfile/BYPlatformWAPI. log"/> 11 <! -- Whether to append to file --> 12 <appendToFile value = "true"/> 13 <! -- Can the name be changed to false? --> 14 <param name = "StaticLogFileName" value = "false"/> 15 <! -- The form of transformation is Date --> 16 <rollingStyle value = "Date"/> 17 <! -- Date format --> 18 <datePattern value = "yyyyMMdd-HHmmss"/> 19 <layout type = "log4net. Layout. PatternLayout"> 20 <! -- <ConversionPattern value = "record time: % date thread ID: [% thread] Log level: %-5 level error class: % logger property: [% property {NDC}]-error Description: % message % newline "/> --> 21 <conversionPattern value =" record time: % date Log level: % level Log class: % logger-Log Description: \ n % message % newline "/> 22 </layout> 23 </appender> 24 25 <! -- Define the output to the console command line --> 26 <appender name = "leleappender" type = "log4net. appender. consoleAppender "> 27 <layout type =" log4net. layout. patternLayout "> 28 <conversionPattern value =" % date [% thread] %-5 level % logger [% property {NDC}]-% message % newline "/> 29 </layout> 30 </appender> 31 <! -- Define output to windows events --> 32 <appender name = "EventLogAppender" type = "log4net. appender. eventLogAppender "> 33 <layout type =" log4net. layout. patternLayout "> 34 <conversionPattern value =" % date [% thread] %-5 level % logger [% property {NDC}]-% message % newline "/> 35 </layout> 36 </appender> 37 <! -- Define the output to the database. Here, for example, the output to the Access database, the database is the log4net. mdb --> 38 <appender name = "AdoNetAppender_Access" type = "log4net. appender. adoNetAppender "> 39 <connectionString value =" Provider = Microsoft. jet. OLEDB.4.0; Data Source = C: log4net. mdb "/> 40 <commandText value =" insert into LogDetails ([LogDate], [Thread], [Level], [Logger], [Message]) VALUES (@ logDate, @ thread, @ logLevel, @ logger, @ message) "/> 41 <! -- Define parameters --> 42 <parameter> 43 <parameterName value = "@ logDate"/> 44 <dbType value = "String"/> 45 <size value = "240 "/> 46 <layout type = "log4net. layout. patternLayout "> 47 <conversionPattern value =" % date "/> 48 </layout> 49 </parameter> 50 <parameter> 51 <parameterName value =" @ thread "/> 52 <dbType value = "String"/> 53 <size value = "240"/> 54 <layout type = "log4net. layout. patternLayout "> 55 <conversionPattern value = "% Thread"/> 56 </layout> 57 </parameter> 58 <parameter> 59 <parameterName value = "@ logLevel"/> 60 <dbType value = "String "/ & gt; 61 & lt; size value = "240"/> 62 & lt; layout type = "log4net. layout. patternLayout "> 63 <conversionPattern value =" % level "/> 64 </layout> 65 </parameter> 66 <parameter> 67 <parameterName value =" @ logger "/> 68 <dbType value = "String"/> 69 <size value = "240"/> 70 <layout type = "log4net. layout. patternLayou T "> 71 <conversionPattern value =" % logger "/> 72 </layout> 73 </parameter> 74 <parameter> 75 <parameterName value =" @ message "/> 76 <dbType value = "String"/> 77 <size value = "240"/> 78 <layout type = "log4net. layout. patternLayout "> 79 <conversionPattern value =" % message "/> 80 </layout> 81 </parameter> 82 </appender> 83 <! -- Define the log output media. The following defines four log output methods. You can also output data of the following type or other types. --> 84 <root> 85 <level value = "ALL"/> 86 <! -- Log recorded in file format --> 87 <appender-ref = "LogFileAppender"/> 88 <! -- Console control display log 89 <appender-ref = "leleappender"/> --> 90 <! -- Windows event log 91 <appender-ref = "EventLogAppender"/> --> 92 <! -- If the corresponding logging is not enabled, you can comment out 93 <appender-ref = "AdoNetAppender_Access"/> 94 --> 95 </root> 96 97 </log4net> 98 </configuration> in this way.Log4net. xml
3. At the bottom of the \ Properties \ AssemblyInfo. cs file, add the following sentence:
[Assembly: log4net. Config. XmlConfigurator (ConfigFile = "log4net. xml", Watch = true)]
1 using System. Reflection; 2 using System. Runtime. CompilerServices; 3 using System. Runtime. InteropServices; 4 5 // The general information about the assembly is controlled through the following feature set 6. Modify these feature values to modify 7 // information associated with the Assembly. 8 [assembly: AssemblyTitle ("Web")] 9 [assembly: AssemblyDescription ("")] 10 [assembly: AssemblyConfiguration ("")] 11 [assembly: assemblyCompany ("")] 12 [assembly: AssemblyProduct ("Web")] 13 [assembly: AssemblyCopyright ("Copyright (C) 2014")] 14 [assembly: assemblyTrademark ("")] 15 [assembly: AssemblyCulture ("")] 16 17 // setting ComVisible to false will make the type 18 in this assembly invisible to COM components. If you need to access a type in this Assembly from COM, 19 // set the ComVisible attribute to true for this type. 20 [assembly: ComVisible (false)] 21 22 // if this project is made public to COM, the following GUID is used for ID23 of the Type Library [assembly: Guid ("940997e2-1820-4f8b-b280-2e1bf%fe27")] 24 25 // The Assembly version information consists of the following four values: 26 // 27 // main version 28 // minor version 29 // internal version 30 // Revision No. 31 // 32 // all these values can be specified, you can also use the default values of revision number and internal version number. 33 // The method is to use "*": 34 [assembly: AssemblyVersion ("1.0.0.0")] as follows: 35 [assembly: AssemblyFileVersion ("1.0.0.0")] 36 37 // Add the following sentence 38 [assembly: log4net. config. xmlConfigurator (ConfigFile = "log4net. xml ", Watch = true)]AssemblyInfo. cs
4. Add global variables to the class to be implemented:
1 public class ConnectController: ApiController 2 {3 # region global variable 4 // <summary> 5 // log reference 6 /// </summary> 7 private static log4net. ILog Log = LogManager. getLogger ("Class Name"); 8 # endregion 9 10 public void Main () 11 {12 try13 {14 Log. debug ("\ n"); 15 Log. debug ("Log started! "); 16} 17 catch (Exception ex) 18 {19 Log. error ("Log exception:", ex); 20} 21 finally22 {23 Log. debug ("Log ended! "); 24} 25} 26}View Code
Personal website is being set up: http://Amoysec.com, ready to use bootstrap + knockoutjs + MVC + WebAPI + EF6.0 to do, of which knockoutjs is also a lot of understanding, although not as good as Uncle Tom of the blog Park, however, I found a lot about a chm document by myself. Welcome to join us!
Reprinted please indicate the sourceThank you: http://www.cnblogs.com/mose/p/Amoy.html