There is no doubt that Log4net is incredibly powerful in journaling.
But there are also areas of criticism, such as the configuration is too cumbersome (here only to say that the log file log, like the logs logged in the database does not involve, the same as)
Have seen in the garden in the packaging of the package into a loghelper, less configuration I did not look carefully, but look at the following comments have said that the log records are still problematic
#8楼 2014-11-17 15:04 aeiiilowx Consult the landlord, using the package Loghelper class write log, in the configuration file layout, with%c,%f or%l to get the log class, The file and line numbers are always the corresponding line numbers for the Loghelper class, which means that the actual log classes, files, and line numbers are not properly obtained. Have you encountered this problem?
This problem I also met, feel uncomfortable, then gave up.
Yesterday suddenly want to solve this problem, say dry do it
1 /// <summary>2 ///Record Log Factory3 /// </summary>4 Public classlogfactory5 {6 /// <summary>7 ///Static Constructors8 /// </summary>9 Staticlogfactory ()Ten { One varelement = Createconfigelement ();//Create XmlElement ALog4net. Config.XmlConfigurator.Configure (Element);//Configure Log4net - } - the /// <summary> - ///Create a ilog for logging - /// </summary> - /// <returns></returns> + Public Staticlog4net. ILog Createlogger () - { +StackTrace st =NewStackTrace (); A vararr = St. GetFrame (1); at returnlog4net. Logmanager.getlogger (arr. GetMethod (). DeclaringType); - } - /// <summary> - ///Create a XmlElement to use for configuration log4net - /// </summary> - /// <returns></returns> in Public StaticXmlElement createconfigelement () - { to varLogDir = configurationmanager.appsettings["logdirectory"] ??"";//get the log directory in the configuration file + if(string. Isnullorwhitespace (LogDir)) - Throw NewDirectoryNotFoundException ("Check the key ' Logdirectory ' in appsettings"); theStringBuilder sb=NewStringBuilder (); *Sb. Append (@"<?xml version= "" 1.0 ""?>"); $ //sb. Append (@ "<configuration>");Panax Notoginseng //sb. Append (@ "<configSections>"); - //sb. Append (@ "<section name=" "Log4net" "type=" "log4net. Config.log4netconfigurationsectionhandler, log4net "/>"); the //sb. Append (@ "</configSections>"); +Sb. Append (@"<log4net>"); ASb. Append (@"<appender name= "" Myappender "" type= "" log4net. Appender.rollingfileappender,log4net "" >"); theSb. Append (@"<param name= "" "File" "Value= " ""+logdir+@"""/>"); +Sb. Append (@"<param name= "" Appendtofile "" value= "" true ""/>"); -Sb. Append (@"<param name= "" Rollingstyle "" value= "" Composite ""/>"); $Sb. Append (@"<param name= "" Datepattern "" value= "" Yyyy-mm-dd/hh". log" "" />"); $Sb. Append (@"<param name= "" maximumFileSize "" value= "" 2MB ""/>"); -Sb. Append (@"<param name= "" Maxsizerollbackups "" value= "" ""/>"); -Sb. Append (@"<param name= "" Staticlogfilename "" value= "" false ""/>"); theSb. Append (@"<layout type= "" Log4net. Layout.patternlayout,log4net "" >"); -Sb. Append (@"<param name= "" Conversionpattern "" value= ""%d [%t]%-5p%c [%x] <%X{auth}>-%n%m%n ""/>");WuyiSb. Append (@"<param name= "header" "Value=" "& #xD;& #xA;----------------------Header-------------------------- & #xD;& #xA; "" />"); theSb. Append (@"<param name= "" Footer "" Value= "" & #xD;& #xA;----------------------Footer-------------------------- & #xD;& #xA; "" />"); -Sb. Append (@"</layout>"); WuSb. Append (@"<filter type= "" Log4net. Filter.levelrangefilter "" >"); -Sb. Append (@"<levelmin value= "INFO" "/>"); AboutSb. Append (@"<levelmax value= "" ERROR ""/>"); $Sb. Append (@"</filter>"); -Sb. Append (@"</appender>"); -Sb. Append (@"<!--All logs --"); -Sb. Append (@"<root>"); ASb. Append (@"<!--log levels for logging: All, DEBUG, INFO, WARN, ERROR, FATAL, OFF ---"); +Sb. Append (@"<priority value= "All" "/>"); theSb. Append (@"<appender-ref ref= "" Myappender ""/>"); -Sb. Append (@"</root>"); $Sb. Append (@"</log4net>"); the //sb. Append (@ "</configuration>"); the theXmlDocument doc=NewXmlDocument (); the Doc. LOADXML (sb.) ToString ()); - returnDoc. documentelement; in } the}logfactory
1 Private Static readonly log4net. ILog Logger = Logfactory.createlogger (); 2 Static void Main (string[] args)3 {4 logger.info (" test message"); 5 }
How to use
Needs to be added within the configuration file
1 < appSettings > 2 < key= "Logdirectory" value= "D:\log\Test\"/> 3 </ appSettings >
----------------------Header--------------------------
2016-11-10 11:42:21,567 [1] INFO _17MCP. Logger.Test.Program [(NULL)] < (NULL) >-
Test message
----------------------Footer--------------------------
Above is the content of the record.
It's a simple thing.
The encapsulated Log4net Portal is in this
Log4net.zip
Package of Log4net