最近需要把用户的一些行为添加到数据库中,所以想到了用log4net ,如果有别的好的方案,大家可以给我指正。
先看一下配置文件 我这个是控制台文件 app.config
<layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{Log_Type}"/> </layout>
这样的是自定义参数
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <root> <level value="ALL"></level> <appender-ref ref="ADONetAppender"/> </root> <!-- SQL数据库--> <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender"> <bufferSize value="1"/> <!-- SQL数据源 ,本地安装SQL客户端--> <connectionType value="System.Data.SqlClient.SqlConnection,System.Data, Version=1.0.3300.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" /> <connectionString value="database=ASM_UserManger;server=xxxxxx;User ID=xxx;Password=xxxx" /> <commandText value="INSERT INTO ASM_Log ([Log_Object],[Log_Type],[Log_Content],[Log_Url],[au_ID]) VALUES (@Log_Object,@Log_Type, @Log_Content, @Log_Url, @au_ID)"/> <parameter> <parameterName value="@Log_Object"/> <dbType value="String"/> <size value="200"/> <layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{Log_Object}"/> </layout> </parameter> <parameter> <parameterName value="@Log_Type"/> <dbType value="String"/> <size value="200"/> <layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{Log_Type}"/> </layout> </parameter> <parameter> <parameterName value="@Log_Content"/> <dbType value="String"/> <size value="2000"/> <layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{Log_Content}"/> </layout> </parameter> <parameter> <parameterName value="@Log_Url"/> <dbType value="String"/> <size value="100"/> <layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{Log_Url}"/> </layout> </parameter> <!-- 自定义成员 --> <parameter> <parameterName value="@au_ID" /> <dbType value="Int32" /> <layout type="LogTest.MyLayout, LogTest" > <param name="ConversionPattern" value="%property{au_ID}"/> </layout> </parameter> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
MyLayout 是一个映射类
public class MyLayout : PatternLayout { public MyLayout() { this.AddConverter("property", typeof(MyMessagePatternConverter)); } } 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; } }
控制台文
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LogTest { class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); log.Info(new ASM_Log { au_ID = 1, Log_Content = "11", Log_Type = "s", Log_Object = "3", Log_Url = "sss" }); Console.ReadKey(); } } public class ASM_Log { public int au_ID { set; get; } public string Log_Object { set; get; } public string Log_Type { set; get; } public string Log_Content { set; get; } public string Log_Url { set; get; } } }
因为项目和别的项目相连,所以有需要的需要引用现有项目就可以
下载链接:http://pan.baidu.com/s/1jHJgLnS