• 1、源代码

using log4net;

using log4net.Layout;

using log4net.Layout.Pattern;

using System.Reflection;

namespace Logger

{

/// <summary>

/// 包含了所有的自定字段属性

/// </summary>

public class LogContent

{

public LogContent(string userName, string actionClick, string logText)

{

UserName = userName;

ActionClick = actionClick;

Message = logText;

}

/// <summary>

/// 系统登陆用户

/// </summary>

public string UserName { get;set; }

/// <summary>

/// 动作事件

/// </summary>

public string ActionClick { get; set; }

/// <summary>

/// 日志描述信息

/// </summary>

public string Message { get; set; }

}

public class CustomLayout : PatternLayout

{

public CustomLayout()

{

this.AddConverter("property", typeof(LogInfoPatternConverter));

}

}

public class LogInfoPatternConverter : PatternLayoutConverter

{

protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)

{

if (Option != null)

{

WriteObject(writer, loggingEvent.Repository, LookupProperty(Option, loggingEvent));

}

else

{

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;

}

}

public class DBLog

{

private static ILog log;

private static ILog DBLogger

{

get

{

if (log == null)

log = LogManager.GetLogger("AppToDB");

return log;

}

}

public static void Info(string userName,string actionClick,string logText)

{

DBLogger.Info(new LogContent(userName,actionClick,logText ) );

}

}

}

2、web.config中的配置

如果数据库中表结构字段类型为nvarchar(max)时,则注意size中value值为-1,如:

<dbType value="String" />

<size value="-1" />

具体配置如下:

<log4net>

<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">

<bufferSize value="1" />

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

<connectionString value="server=HPWIN10;database=MES_TEST;Integrated Security=True" />

<commandText value="INSERT INTO [dbo].SH_P3Log (LogTime,LogThread,LogMessage,ActionClick,UserName) values(@log_time,@log_thread,@log_message,@action_click,@user_name)" />

<parameter>

<parameterName value="@log_time" />

<dbType value="DateTime" />

<layout type="log4net.Layout.RawTimeStampLayout" />

</parameter>

<parameter>

<parameterName value="@log_thread" />

<dbType value="String" />

<size value="10" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%thread" />

</layout>

</parameter>

<parameter>

<parameterName value="@log_message" />

<dbType value="String" />

<size value="-1" />

<layout type="Logger.CustomLayout">

<conversionPattern value="%property{Message}" />

</layout>

</parameter>

<parameter>

<parameterName value="@action_click" />

<dbType value="String" />

<size value="50" />

<layout type="Logger.CustomLayout">

<conversionPattern value="%property{ActionClick}" />

</layout>

</parameter>

<parameter>

<parameterName value="@user_name" />

<dbType value="String" />

<size value="30" />

<layout type="Logger.CustomLayout">

<conversionPattern value="%property{UserName}" />

</layout>

</parameter>

</appender>

<logger name="AppToDB" additivity="false">

<priority value="INFO" />

<appender-ref ref="ADONetAppender" />

</logger>

</log4net>

3、代码调用

Logger.DBLog.Info("张工","删除订单","删除订单:"+PONo);

posted on 2022-03-18 09:50  蒙蒙浮霁月  阅读(61)  评论(0编辑  收藏  举报