需要对输出日志增加一个参数的时候,对应的TGLOG.DLL,需要重新增加参数:
ILog log1 = LogManager.GetLogger("ReflectionLayout");
ILog log2 = LogManager.GetLogger("HashtableLayout");
ILog log3 = LogManager.GetLogger("MyLayout");
IMyLog myLog = MyLogManager.GetLogger("ExpandILog");
log1.Error(new LogMessage(001,"查询",(int)TGLog.ActionType.View,"这是新加参数测试","192.168.1.74","qi","ie8.0","测试多一个参数"));
log2.Error(new LogMessage(001,"查询",(int)TGLog.ActionType.View,"这是新加参数测试","192.168.1.74","qi","ie8.0","测试多一个参
数").ToHashtable());
log3.Fatal(new LogMessage(1,"操作对象:0",(int)TGLog.ActionType.Other,"这是四个参数测试","192.168.1.1","MyComputer","Maxthon
(MyIE2)Fans", "测试多一个参数"));
myLog.Fatal(0, "操作对象1", 4, "这是消息内容", "192.168.1.15", "IE7.0", "myComputer", ec);
从上面对象调用方法可以看出:增加一个新参数,必须在类LogMessage中增加新参数的属性,并且重新构造函数;
如:
/// <summary>
/// 错误信息
/// </summary>
public string Error
{
get;
set;
}
///构造函数
public LogMessage(
int operatorID,
string operand,
int ActionType,
string message,
string ip,
string machineName,
string browser,
string error
)
{
this.ActionType = ActionType;
this.Operator = operatorID;
this.Message = message;
this.Operand = operand;
this.IP = ip;
this.Browser = browser;
this.MachineName = machineName;
this.Error = error;
}
/// <summary>
/// 得到哈希表
/// </summary>
/// <returns></returns>
public Hashtable ToHashtable()
{
Hashtable ht = new Hashtable();
ht.Add("Operator", this.Operator);
ht.Add("Message", this.Message);
ht.Add("ActionType", this.ActionType);
ht.Add("Operand", this.Operand);
ht.Add("IP", this.IP);
ht.Add("MachineName", this.MachineName);
ht.Add("Browser", this.Browser);
ht.Add("Error", this.Error);
return ht;
}
1、对象log1和log2,对应配置文件方法为:“ReflectionLayout”,“HashtableLayout”,在配置文件中 对应的记录格式为:
TGLog.ExpandLayout2.ReflectionLayout,TGLog.ExpandLayout2.HashtableLayout,在 这两个类中不需要在做任何的修改;只需要在配置文件中声
明新参数,并且修改日志输出格式即可:
<!--错误-->
<parameter>
<parameterName value="@error" />
<dbType value="String" />
<size value="3000" />
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
<conversionPattern value="%property{Error}" />
</layout>
</parameter>
“Error”是LogMessage类中新增加参数的名称;
<!--记录的格式。-->
<layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">
<param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%
property{Operator} 操作类型:%property{Action}%n 当前机器名:%property%n当前机器名及登录用户:%username %n 记录位置:%location%n 消
息描述:%property{Message}%n 异常:%exception%n 消息:%message 错误:%property{Error}%newline%n%n" />
</layout>
2.对象log3,对应配置文件为:“MyLayout”,在配置文件中对应的记录格式为:
TGLog.ExpandLayout1.MyLayout,这个类是继承“log4net.Layout.PatternLayout”类,需要在构造函数中增加参数:如:
this.AddConverter("Error", typeof(ErrorPatternConverter)); //错误
同时必须在TGLog.ExpandLayout1.Layout中增加“ErrorPatternConverter”类:如下
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Layout.Pattern;
using System.IO;
using log4net.Core;
using TGLog;
namespace TGLog.Layout
{
/// <summary>
/// 操作者
/// </summary>
internal sealed class ErrorPatternConverter : PatternLayoutConverter
{
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
LogMessage logMessage = loggingEvent.MessageObject as LogMessage;
if (logMessage != null)
{
// 将UserName作为日志信息输出
writer.Write(logMessage.Error);
}
}
}
在配置文件修改MyLayOut对应的记录格式:
<!--记录的格式。-->
<layout type="TGLog.ExpandLayout1.MyLayout,TGLog">
<param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%
Operator 操作类型:%ActionType%n 当前机器名:%property%n当前机器名及登录用户:%username %n 记录位置:%location%n 消息描述:%
Message%n 消息:%message 错误:%property{Error}%newline%n%n" />
</layout>
</appender>
3.myLog 对象是调用 using TGLog.ExpandILog中MyLogManager类。
MyILog是继承了ILOG的接口,并且重新定义了接口方法
如:
void Debug(int operatorID, string operand, int actionType, object message, string ip, string browser, string machineName);
void Debug(int operatorID, string operand, int actionType,object message, string ip, string browser, string machineName, Exception
t);
MyLogImpl 是实现上面接口方法的类。
如果增加了新参数 ,必须在接口中增加新参数,并且在MyLogImpl类中实现;
如:
public void Debug(int operatorID, string operand, int actionType,object message, string ip, string browser, string machineName,
System.Exception t)
{
if (this.IsDebugEnabled)
{
LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info,
message, t);
loggingEvent.Properties["Operator"] = operatorID;
loggingEvent.Properties["Operand"] = operand;
loggingEvent.Properties["ActionType"] = actionType;
loggingEvent.Properties["IP"] = ip;
loggingEvent.Properties["Browser"] = browser;
loggingEvent.Properties["MachineName"] = machineName;
Logger.Log(loggingEvent);
}
}
然后在配置文件中修改记录格式:
<!--记录的格式。-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%
property{Operator} 操作类型:%property{ActionType}%n 当前机器名:%property%n当前机器名及登录用户:%username %n 记录位置:%
location%n 消息描述:%property{Message}%n 异常:%exception%n 消息:%message 错误:%property{Error}%newline%n%n" />
</layout>
上面是好几种方法,可以灵活应用;