log4net 添加自定义日志到数据库

时间:2021-10-09 17:45:51

添加操作日志到数据库举例:

(一)建立数据库的操作日志表,如下我建立了一个简单的日志表

log4net  添加自定义日志到数据库

(二)配置文件中的配置如下

  <log4net>
<!--错误日志记录数据库-->
<logger name="OperateDB">
<level value="INFO"/>
<appender-ref ref="AdoNetAppender_OperToSql" />
</logger>
<!--操作日志记录到数据库-->
<appender name="AdoNetAppender_OperToSql" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=192.168.1.188\MSSQLSERVER2008;database=HotelMIS;uid=sa;pwd=123456;" />
<commandText value="insert into OperateLog(LogLevel, Msg, ClassName, Method, Cby, Cdt)values(@LogLevel, @Msg, @ClassName, @Method, @Cby, @Cdt)" />
<bufferSize value="1" /> <parameter>
<parameterName value="@LogLevel" />
<dbType value="String" />
<size value="64" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter> <parameter>
<parameterName value="@Msg" />
<dbType value="String" />
<size value="10240" />
<layout type="Util.MyLogLayout, Util">
<conversionPattern value="%property{Msg}" />
</layout>
</parameter> <parameter>
<parameterName value="@ClassName" />
<dbType value="String" />
<size value="256" />
<layout type="Util.MyLogLayout, Util" >
<param name="ConversionPattern" value="%property{ClassName}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@Method" />
<dbType value="String" />
<size value="256" />
<layout type="Util.MyLogLayout, Util" >
<param name="ConversionPattern" value="%property{Method}"/>
</layout>
</parameter> <parameter>
<parameterName value="@Cby" />
<dbType value="String" />
<size value="128" />
<layout type="Util.MyLogLayout, Util" >
<param name="ConversionPattern" value="%property{Cby}"/>
</layout>
</parameter> <parameter>
<parameterName value="@Cdt" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
</appender> </log4net>

(三)定义自己的 Layout 布局类 和 MyMessagePatternConverter类

namespace Util
{
public class MyLogLayout : PatternLayout
{
public MyLogLayout()
{
this.AddConverter("property", typeof(MyMessagePatternConverter));
} }
}
namespace Util
{
public class MyMessagePatternConverter : PatternLayoutConverter
{
protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
{
if (Option != null)
{
// Write the value for the specified key
WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));
}
else
{
// Write all the key value pairs
WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
}
}
/// <summary>
/// 通过反射获取传入的日志对象的某个属性的值
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
private object LookupProperty(string property, log4net.Core.LoggingEvent loggingEvent)
{
object propertyValue = string.Empty;
PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
if (propertyInfo != null)
propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);
return propertyValue;
}
}
}

(四)最后可以调用了,我这里写了一个公共的类Log

[assembly: log4net.Config.XmlConfigurator(Watch = false)]
namespace Util
{
public class Log
{
   #region 记录操作日志到数据库
/// <summary>
/// 记录操作日志到数据库
/// </summary>
/// <param name="user"></param>
/// <param name="msg"></param>
/// <param name="className"></param>
/// <param name="method"></param>
public static void OperateDB(string user, string msg, string className, string method)
{
ILog logger = LogManager.GetLogger("OperateDB");
logger.Info(new LogContent() { Msg = msg, Cby = user, ClassName = className, Method = method });//将异常信息写到磁盘上.
} /// <summary>
/// 记录操作日志到数据库
/// </summary>
/// <param name="user"></param>
/// <param name="msg"></param>
public static void OperateDB(string user, string msg)
{
ILog logger = LogManager.GetLogger("OperateDB");
logger.Info(new LogContent() { Msg = msg, Cby = user, ClassName = null, Method = null });//将异常信息写到磁盘上.
}
#endregion } }