Log4Net日志 01 跑起来
引用程序集
Common.Logging.Log4Net1211
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Common.Logging" version="3.4.1" targetFramework="net45" /> <package id="Common.Logging.Core" version="3.4.1" targetFramework="net45" /> <package id="Common.Logging.Log4Net1211" version="3.4.1" targetFramework="net45" /> <package id="log4net" version="2.0.0" targetFramework="net45" /> </packages>
从上述文件可以看出,Common.Logging.Log4Net1211依赖下列三个程序集
<package id="Common.Logging" version="3.4.1" targetFramework="net45" /> <package id="Common.Logging.Core" version="3.4.1" targetFramework="net45" /> <package id="log4net" version="2.0.0" targetFramework="net45" />
第一步:
引用:Common.Logging.Log4Net1211

第二步:配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--配置节点-->
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<!--配置节点-->
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
<!--
inline :log4net 节点在App.Config/Web.Config 文件中配置
file:使用外置配置文件 (需要configFile参数配合使用,<arg key="configFile" value="外部配置文件")
file-watch: 与"file"一样,只是多了一个监视外部配置文件的变动功能,如果有变动则重新加载配置。
<arg key="configFile" value="D:\SVN\物通天下小程序接口\WxApi\App_Data\Config\log4net.xml" />
或
<arg key="configFile" value="~/App_Data/Config/log4net.xml" />
external:IBatis将不会尝试配置Log4Net。
-->
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="D:\SPRINT.NET\容器\Common.Logging.Log4Net1211\config\log4net.xml" />
</factoryAdapter>
</logging>
</common>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 4 <configSections> 5 6 7 <!--配置节点--> 8 <sectionGroup name="common"> 9 <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> 10 </sectionGroup> 11 </configSections> 12 13 <!--配置节点--> 14 <common> 15 <logging> 16 <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211"> 17 <!-- 18 inline :log4net 节点在App.Config/Web.Config 文件中配置 19 file:使用外置配置文件 (需要configFile参数配合使用,<arg key="configFile" value="外部配置文件") 20 file-watch: 与"file"一样,只是多了一个监视外部配置文件的变动功能,如果有变动则重新加载配置。 21 external:IBatis将不会尝试配置Log4Net。 22 --> 23 <arg key="configType" value="FILE-WATCH" /> 24 <arg key="configFile" value="D:\SPRINT.NET\容器\Common.Logging.Log4Net1211\config\log4net.xml" /> 25 </factoryAdapter> 26 </logging> 27 </common> 28 29 <startup> 30 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 31 </startup> 32 </configuration>

第三步:配置日志XML文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <log4net> 3 4 <!-- 错误日志类--> 5 <logger name="logerror"> 6 <level value="ALL" /> 7 <appender-ref ref="ErrorAppender" /> 8 </logger> 9 <!-- 信息日志类 --> 10 <logger name="loginfo"> 11 <level value="ALL" /> 12 <appender-ref ref="InfoAppender" /> 13 </logger> 14 15 <!-- 错误日志附加介质--> 16 <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> 17 <param name="File" value="Log\\LogError\\" /> 18 <param name="AppendToFile" value="true" /> 19 <param name="MaxSizeRollBackups" value="100" /> 20 <param name="MaxFileSize" value="10240" /> 21 <param name="StaticLogFileName" value="false" /> 22 <param name="DatePattern" value="yyyyMMdd".htm"" /> 23 <param name="RollingStyle" value="Date" /> 24 <!--布局--> 25 <layout type="log4net.Layout.PatternLayout"> 26 <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 27 </layout> 28 </appender> 29 30 <!-- 信息日志附加介质--> 31 <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> 32 <param name="File" value="Log\\LogInfo\\" /> 33 <param name="AppendToFile" value="true" /> 34 <param name="MaxFileSize" value="10240" /> 35 <param name="MaxSizeRollBackups" value="100" /> 36 <param name="StaticLogFileName" value="false" /> 37 <param name="DatePattern" value="yyyyMMdd".htm"" /> 38 <param name="RollingStyle" value="Date" /> 39 <!-- 信息日志布局--> 40 <layout type="log4net.Layout.PatternLayout"> 41 <param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> 42 </layout> 43 </appender> 44 45 </log4net>
第四步:调用
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Common.Logging.Log4Net1211 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 ILog log = LogManager.GetLogger("logerror"); 14 log.Info("logerror"); 15 } 16 } 17 }
对应项目文件

浙公网安备 33010602011771号