🥝 NuGet引用log4net
🥝 Configs文件夹下创建log4net.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logs\\" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMM\\"info_"yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="***************************************************************************************************************************************************%n[%d] [%p] [%F Line:%L] %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender name="DebugRollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logs\\" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMM\\"debug_"yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="***************************************************************************************************************************************************%n[%d] [%p] [%F Line:%L] %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <appender name="WarnRollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logs\\" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMM\\"warn_"yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="***************************************************************************************************************************************************%n[%d] [%p] [%F Line:%L] %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN" /> <param name="LevelMax" value="WARN" /> </filter> </appender> <appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logs\\" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMM\\"error_"yyyy-MM-dd".log"" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="***************************************************************************************************************************************************%n[%d] [%p] [%F Line:%L] %m%n" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Error" /> <param name="LevelMax" value="FATAL" /> </filter> </appender> <root> <appender-ref ref="InfoRollingLogFileAppender" /> <appender-ref ref="DebugRollingLogFileAppender" /> <appender-ref ref="WarnRollingLogFileAppender" /> <appender-ref ref="ErrorRollingLogFileAppender" /> </root> </log4net> </configuration> |
🥝 AssemblyInfo.cs文件下添加
1 |
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Configs\log4net.config", Watch = true)] |
🥝 类中引用
1 |
private ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |