标签:nlog ref public stat text 默认 override style data
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> </configSections> <log4net> <root> <level value="ALL" /> <appender-ref ref="rollingFile" /> <appender-ref ref="ADONetAppender"/> </root> <logger name="Presin_log"> <level value="ALL" /> <appender-ref ref="PresinLoging" /> </logger> <logger name="Oracle_DB"> <level value="ALL" /> <appender-ref ref="OracleDB" /> </logger> <logger name="flowseting_log"> <level value="ALL" /> <appender-ref ref="flowsetingLoging" /> </logger> <!--Presin日志--> <appender name="PresinLoging" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Log\Presin\" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Composite" /> <param name="DatePattern" value="yyyy-MM-dd.‘.txt‘" /> <param name="MaximumFileSize" value ="5MB"/> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%date %-5level %newline %logger - %message%newline %newline" /> </layout> </appender> <appender name="OracleDB" type="log4net.Appender.AdoNetAppender,log4net" > <bufferSize value="20"/> <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <connectionString value="Data Source=ORCl;User ID=sa;Password=sa;" /> <commandText value="INSERT INTO LogDetails (LogDate,loglevel,loglogger,LogMessage,logfunctionname,logbusinessname) VALUES (:log_date,:log_level,:logger,:message,:FunctionName,:BusinessName)" /> <parameter> <parameterName value=":log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}" /> </layout> </parameter> <parameter> <parameterName value=":log_level" /> <dbType value="String" /> <size value="200" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%p" /> </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="2000" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{Message}" /> </layout> </parameter> <parameter> <parameterName value=":FunctionName" /> <dbType value="String" /> <size value="200" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{FunctionName}" /> </layout> </parameter> <parameter> <parameterName value=":BusinessName" /> <dbType value="String" /> <size value="200" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{BusinessName}" /> </layout> </parameter> </appender> <!--flowseting日志--> <appender name="flowsetingLoging" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Log\flowseting\" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="StaticLogFileName" value="true" /> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%date %-5level %newline %logger - %message%newline %newline" /> </layout> </appender> </log4net> </configuration>
<root> <level value="ALL" /> <appender-ref ref="rollingFile" /> <appender-ref ref="ADONetAppender"/> </root>
<logger name="Oracle_DB"> <level value="ALL" /> <appender-ref ref="OracleDB" /> </logger>
<appender name="OracleDB" type="log4net.Appender.AdoNetAppender,log4net" > <bufferSize value="20"/> <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <connectionString value="Data Source=ORCl;User ID=sa;Password=sa;" /> <commandText value="INSERT INTO LogDetails (LogDate,loglevel,loglogger,LogMessage,logfunctionname,logbusinessname) VALUES (:log_date,:log_level,:logger,:message,:FunctionName,:BusinessName)" /> <parameter> <parameterName value=":log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}" /> </layout> </parameter> <parameter> <parameterName value=":log_level" /> <dbType value="String" /> <size value="200" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%p" /> </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="2000" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{Message}" /> </layout> </parameter> <parameter> <parameterName value=":FunctionName" /> <dbType value="String" /> <size value="200" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{FunctionName}" /> </layout> </parameter> <parameter> <parameterName value=":BusinessName" /> <dbType value="String" /> <size value="200" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{BusinessName}" /> </layout> </parameter> </appender>
<parameter> <parameterName value=":log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}" /> </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=":FunctionName" /> <dbType value="String" /> <size value="200" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{FunctionName}" /> </layout> </parameter>
<parameter> <parameterName value=":message" /> <dbType value="String" /> <size value="2000" /> <layout type="MapgisEgovLog4_WriteLog.MyLayout"> <conversionPattern value="%property{Message}" /> </layout> </parameter>
/// <summary> /// 包含了所有的自定字段属性 /// </summary> public class LogContent { public LogContent(string sFunctionName, string sLogMessasge,string sLogBusinessName) { FunctionName = sFunctionName; Message = sLogMessasge; BusinessName = sLogBusinessName; } /// <summary> /// 方法名称 /// </summary> public string FunctionName { get; set; } /// <summary> /// 日志描述信息 /// </summary> public string Message { get; set; } /// <summary> /// 业务名称 /// </summary> public string BusinessName { get; set; } } public class MyLayout : PatternLayout { public MyLayout() { this.AddConverter("property", typeof(LogInfoPatternConverter)); } } public class LogInfoPatternConverter : PatternLayoutConverter { protected override void Convert(TextWriter writer, 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 LogContent { public LogContent(string sFunctionName, string sLogMessasge,string sLogBusinessName) { FunctionName = sFunctionName; Message = sLogMessasge; BusinessName = sLogBusinessName; } /// <summary> /// 方法名称 /// </summary> public string FunctionName { get; set; } /// <summary> /// 日志描述信息 /// </summary> public string Message { get; set; } /// <summary> /// 业务名称 /// </summary> public string BusinessName { get; set; } }
public class MyLayout : PatternLayout { public MyLayout() { this.AddConverter("property", typeof(LogInfoPatternConverter)); } }
public class LogInfoPatternConverter : PatternLayoutConverter { protected override void Convert(TextWriter writer, 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; } }
调用示例
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(strPath + "配置文件.xml")); ILog log = LogManager.GetLogger("Oracle_DB"); log.Info(new LogContent("Button1_Click", "测试成功","测试"));
参考:
http://blog.csdn.net/kongwei521/article/details/52242319
标签:nlog ref public stat text 默认 override style data
原文地址:http://www.cnblogs.com/wenpeng/p/6857642.html