log4net日志记录
2018-07-09 本文已影响15人
落地成佛
1、1.在assemblyInfo.cs添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
2、2.在根目录添加log4net.config文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<logger name="log" additivity="false">
<level value="ALL"/>
<appender-ref ref="SysAppender" />
</logger>
<!--Oracle数据库-->
<appender name="OracleAppender" type="log4net.Appender.AdoNetAppender">
<!-- Oracle数据源-->
<connectionType value="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<!-- Oracle连接字符串-->
<connectionString value="DATA SOURCE=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.206)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)));PASSWORD=watdb;PERSIST SECURITY INFO=True;USER ID=watdb;"/>
<commandText value="INSERT INTO SYS_LOG(Dates,Levels,Logger,Message,Exception,ClientUser,ClientIP,RequestUrl,Action)VALUES(:Dates,:Levels,:Logger,:Message,:Exception,:ClientUser,:ClientIP,:RequestUrl,:Action)"/>
<!--
设置缓存区大小
1表明有一条日志就要写入
如果10就表示日志到达10条时一起写入
-->
<bufferSize value="0"/>
<parameter>
<parameterName value=":Dates" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value=":Levels" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":Logger" />
<dbType value="String" />
<size value="200" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":Message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value=":Exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%exception" />
</layout>
</parameter>
<!--DIY-->
<parameter>
<parameterName value=":ClientUser" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ClientUser}" />
</layout>
</parameter>
<parameter>
<parameterName value=":ClientIP" />
<dbType value="String" />
<size value="20" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ClientIP}" />
</layout>
</parameter>
<parameter>
<parameterName value=":RequestUrl" />
<dbType value="String" />
<size value="500" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RequestUrl}" />
</layout>
</parameter>
<parameter>
<parameterName value=":Action" />
<dbType value="String" />
<size value="20" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Action}" />
</layout>
</parameter>
</appender>
<!--Sqlite数据库-->
<appender name="SqliteAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<connectionString value="Data Source=|DataDirectory|test.db;Version=3;" />
<commandText value="INSERT INTO Log (Date, Level, Logger, ClientUser,ClientIP, RequestUrl,Action, Message, Exception) VALUES (@Date, @Level, @Logger,@ClientUser,@ClientIP, @RequestUrl,@Action, @Message, @Exception)" />
<parameter>
<parameterName value="@Date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@Level" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@Logger" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@ClientUser" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ClientUser}" />
</layout>
</parameter>
<parameter>
<parameterName value="@ClientIP" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ClientIP}" />
</layout>
</parameter>
<parameter>
<parameterName value="@RequestUrl" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RequestUrl}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Action" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Action}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%exception" />
</layout>
</parameter>
</appender>
<!--SqlServer数据库-->
<appender name="SqlServerAppender" type="log4net.Appender.AdoNetAppender">
<!-- SqlServer数据源-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!-- SqlServer连接字符串-->
<connectionString value="data source=.;initial catalog=wkmvc_db;integrated security=false;persist security info=True;User ID=sa_wkmvc;Password=123456" />
<connectionString value="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\wkmvc_db.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" />
<commandText value="INSERT INTO SYS_LOG(Dates,Levels,Logger,ClientUser,ClientIP,RequestUrl,Action,Message,Exception)VALUES(@Dates,@Levels,@Logger,@ClientUser,@ClientIP,@RequestUrl,@Action,@Message,@Exception)"/>
<!--
设置缓存区大小
1表明有一条日志就要写入
如果10就表示日志到达10条时一起写入
-->
<bufferSize value="0"/>
<parameter>
<parameterName value="@Dates" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@Levels" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@Logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<!--DIY-->
<parameter>
<parameterName value="@ClientUser" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%property{ClientUser}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@ClientIP" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%property{ClientIP}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@RequestUrl" />
<dbType value="String" />
<size value="500" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%property{RequestUrl}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@Action" />
<dbType value="String" />
<size value="100" />
<layout type="log4net.Layout.PatternLayout" >
<param name="ConversionPattern" value="%property{Action}"/>
</layout>
</parameter>
</appender>
<!--记录到文件-->
<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="datalog" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="\\yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d - %m%n" />
<param name="Header" value="
----------------------begin--------------------------
" />
<param name="Footer" value="
----------------------end--------------------------
" />
</layout>
</appender>
</log4net>
</configuration>
<!--
调用实例
log4net.ILog log = log4net.LogManager.GetLogger("Filelog");
log.Info(Message);
%m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息
%n(new line):换行
%d(datetime):输出当前语句运行的时刻
%r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
%t(thread id):当前语句所在的线程ID
%p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
%c(class):当前日志对象的名称
%L:输出语句所在的行号
%F:输出语句所在的文件名
%-数字:表示该项的最小长度,如果不够,则用空格填充
例如,转换模式为%r [%t]%-5p %c - %m%n 的 PatternLayout 将生成类似于以下内容的输出:
176 [main] INFO org.foo.Bar - Located nearest gas station.
-->
3、可定义个日志帮助类,如下:
public static class LogHelper
{
static ILog log = LogManager.GetLogger("log");
public static void Save(string content,Exception ex)
{
log.Error(content, ex);
}
public static void Save(string content)
{
log.Error(content);
}
public static void SaveAsyn(string content)
{
Task.Run( ()=> log.Error(content));
}
public static void SaveAsyn(string content,Exception ex)
{
Task.Run(() => log.Error(content, ex));
}
}
3 按日期及文件大小記錄
<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="datalog" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="\\yyyy-MM-dd".log"" />
<param name="StaticLogFileName" value="false" />
<param name="MaximumFileSize" value="1MB" />
<param name="MaxSizeRollBackups" value="-1" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d - %m%n" />
<param name="Header" value="
----------------------begin--------------------------
" />
<param name="Footer" value="
----------------------end--------------------------
" />
</layout>
</appender>