去下载log4net.dill
Apache log4net – Download Apache log4net - Apache log4net
下载完拷到项目DEBUG目录下,引用下右击选择添加引用,选择 刚拷贝过来的log4net.dill
配置文件代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Level的级别,由高到低 -->
<!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
<!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
<log4net>
<logger name="SysRFLogger">
<level value="DEBUG" />
<appender-ref ref="SysRFAppender" />
</logger>
<logger name="DebugRFLogger">
<level value="DEBUG" />
<appender-ref ref="DebugAppender" />
</logger>
<logger name="MsgLogger">
<level value="DEBUG" />
<appender-ref ref="MsgAppender" />
</logger>
<logger name="OperInfoLogger">
<level value="DEBUG" />
<appender-ref ref="OperInfoAppender" />
</logger>
<appender name="SysRFAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\syslog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\sysDebuglog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="MsgAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Messagelog.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="OperInfoAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.30319.18020, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<commandText value="INSERT INTO LOG_USEROPERLOG ([ID],[Date],[User],[Flag],[OperInfo],[OperFlag],[OperResult],[Message]) VALUES (@Id,@log_date, @user, @flag, @operinfo,@operflag, @operresult, @message)" />
<parameter>
<parameterName value="@Id" />
<dbType value="String" />
<size value="36" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{ID}" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@user" />
<dbType value="String" />
<size value="64" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{User}" />
</layout>
</parameter>
<parameter>
<parameterName value="@flag" />
<dbType value="Int32" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{Flag}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operinfo" />
<dbType value="String" />
<size value="255" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperInfo}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operflag" />
<dbType value="String" />
<size value="32" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperFlag}" />
</layout>
</parameter>
<parameter>
<parameterName value="@operresult" />
<dbType value="String" />
<size value="255" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{OperResult}" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
<param name="ConversionPattern" value="%property{Message}" />
</layout>
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="SysRFAppender" />
</root>
</log4net>
</configuration>
配置文件属性更改为 如果较新则复制
在AssemblyInfo.cs文件中加入这条语句
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
代码添加使用这个log4net库
using log4net;
代码里添加一个类
public static class log4net
{
public static readonly ILog loginfo = LogManager.GetLogger("loginfo");
public static readonly ILog logerror = LogManager.GetLogger("logerror");
public static void WriteLog(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
public static void WriteLog(string info, Exception se)
{
if (logerror.IsErrorEnabled)
{
logerror.Error(info, se);
}
}
}
测试一下:
log4net.WriteLog("log4net 测试输出");