Log4net is integrated into the MVC + EF framework, and log4netmvc
References the Log4Net. dll file
1,
[Assembly: log4net. Config. XmlConfigurator (ConfigFile = "Web. config", Watch = true)]
The above code is written to the AssemblyInfo. cs file.
2. In the Global. asax file, add the initialization code for Log4Net to the Application_Start method as follows:
String l4net = Server. MapPath ("~ /Web. config ");
Log4net. Config. XmlConfigurator. ConfigureAndWatch (new System. IO. FileInfo (l4net ));
3. Configure the web. config file. The content is as follows. For some configuration instructions, see the notes.
1
<! -- This configuration item must be placed at the first node of configuration -->
<ConfigSections> 2 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 3 </configSections> 4 5 <log4net debug =" true "> 6 <appender name =" ErrorAppender "type =" log4net. appender. rollingFileAppender "> 7 <filter type =" log4net. filter. levelMatchFilter "> 8 <levelToMatch value =" ERROR "/> 9 </filter> 10 <filter type =" log4net. filter. denyAllFilter "/> 11 <! -- Log file name start with --> 12 <file value = ".. \ Logs \ Error \"/> 13 <! -- Whether to append to a file --> 14 <appendToFile value = "true"/> 15 <! -- Use the date and file size to change the log file name. --> 16 <rollingStyle value = "Composite"/> 17 <! -- Maximum number of transformations --> 18 <maxSizeRollBackups value = "10"/> 19 <! -- Maximum file size --> 20 <maximumFileSize value = "500KB"/> 21 <! -- If the value of StaticLogFileName in date format is true and 22, the File name of the Current Log File (relative to the historical log File) is only the File parameter. 23 if it is false, the File name is File + DatePattern. --> 24 <staticLogFileName value = "false"/> 25 <! -- The following format logs can be archived by month --> 26 <datePattern value = "yyyy-MM/yyyy-MM-dd. 'log' "/> 27 <layout type =" log4net. layout. patternLayout "> 28 <conversionPattern value =" % date % logger: % message % newline "/> 29 </layout> 30 </appender> 31 <appender name =" InfoAppender "type =" log4net. appender. rollingFileAppender "> 32 <filter type =" log4net. filter. levelMatchFilter "> 33 <levelToMatch value =" INFO "/> 34 </filter> 35 <! -- Logs that do not meet the info condition are not output --> 36 <filter type = "log4net. Filter. DenyAllFilter"/> 37 <! -- Start with log file name --> 38 <file value = ".. \ Logs \ Info \"/> 39 <! -- Whether to append to a file --> 40 <appendToFile value = "true"/> 41 <! -- Use the date and file size to change the log file name. --> 42 <rollingStyle value = "Composite"/> 43 <! -- Maximum number of transformations --> 44 <maxSizeRollBackups value = "10"/> 45 <! -- Maximum file size --> 46 <maximumFileSize value = "500KB"/> 47 <! -- Date format --> 48 <staticLogFileName value = "false"/> 49 <datePattern value = "yyyy-MM-dd"/> 50 <layout type = "log4net. layout. patternLayout "> 51 <conversionPattern value =" % date % logger: % message % newline "/> 52 </layout> 53 </appender> 54 <root> 55 <level value =" All "/> 56 <appender-ref =" ErrorAppender "/> 57 <appender-ref =" InfoAppender "/> 58 </root> 59 </log4net>
4. Add the LogHelper. cs File
1 public class LogHelper 2 { 3 public static void debug(string message) 4 { 5 log4net.ILog log = log4net.LogManager.GetLogger("LogOut"); 6 if (log.IsDebugEnabled) 7 { 8 log.Debug(message); 9 }10 log = null;11 }12 13 public static void error(string message)14 {15 log4net.ILog log = log4net.LogManager.GetLogger("LogOut");16 if (log.IsErrorEnabled)17 {18 log.Error(message);19 }20 log = null;21 }22 23 public static void fatal(string message)24 {25 26 log4net.ILog log = log4net.LogManager.GetLogger("LogOut");27 if (log.IsFatalEnabled)28 {29 log.Fatal(message);30 }31 log = null;32 }33 public static void info(string message)34 {35 log4net.ILog log = log4net.LogManager.GetLogger("LogOut");36 if (log.IsInfoEnabled)37 {38 log.Info(message);39 }40 log = null;41 }42 43 public static void warn(string message)44 {45 log4net.ILog log = log4net.LogManager.GetLogger("LogOut");46 if (log.IsWarnEnabled)47 {48 log.Warn(message);49 }50 log = null;51 }52 }
Now, you can start your Log4net journey.