Use Log4net, log4net
1. add a reference to log4net (first download log4net. dll file) 2 2. instantiate the Log object 3 public log4net in the base class. ILog Log 4 {5 get 6 {7 return log4net. logManager. getLogger ("Sfc. client. api "); 8} 9} 10 public static void WriteLog (Type t, string msg) 11 {12 log4net. ILog log = log4net. logManager. getLogger (t); 13 log. error (msg); 14} 15 3. method to add a Log record 16 Log. debug ("Enter GetRequestPost"); 17 4. in the Web. add log4net configuration 18 <configuration> 19 <configSections> 20 <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> 21 </configSections> 22 <log4net> 23 <! -- Define output to file --> 24 <appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender"> 25 <! -- Define the file storage location --> 26 <file value = "log \"/> 27 <appendToFile value = "true"/> 28 <rollingStyle value = "Date"/> 29 <datePattern value = "yyyy \ yyyyMM \ yyyyMMdd'.txt '"/> 30 <staticLogFileName value = "false"/> 31 <param name = "MaxSizeRollBackups" value = "100 "/> 32 <layout type = "log4net. layout. patternLayout "> 33 <! -- Text description at the end of each log --> 34 <! -- Output format --> 35 <! -- Example: 2008-03-26 13:42:32, 111 [10] INFO Log4NetDemo. mainClass [(null)]-info --> 36 <conversionPattern value = "% newline % n record time: % date % n thread ID: [% thread] % n log level: %-5 level % n error class: % logger property: [% property {NDC}]-% n error description: % message % newline % n "/> 37 </layout> 38 </appender> 39 <root> 40 <level value =" DEBUG "/> 41 <! -- Log recorded as a file --> 42 <appender-ref = "RollingLogFileAppender"/> 43 </root> 44 </log4net> 45 </configuration>
Note: If the log file is not generated, add [assembly: log4net. Config. XmlConfigurator (ConfigFile = "web. config", Watch = true)] to AssemblyInfo. cs under Properties.
Directly add it to the last line. The web. config file here is the file name of the log4net configuration file, but it is also a separate file, such as log4net. conig
Log4net: http:// I .cnblogs.com/Files.aspx