configuration file can refer to the ". NET" Log4net profile resolution in the previous blog post .
First in Global . aspx add such a code to the , to read the configuration file , Initialize log4net the Environment .
public class Global:System.Web.HttpApplication { protected void Application_Start (object sender, EventArgs e) { log4net. Config.XmlConfigurator.Configure (); }
Create a newLogfolder,The required classes under the log are built under this folder.develop a good habit of coding.be aware that the namespaces for these classes must be consistent.
first oneLogMessageclass.
This class includes a custom attribute , is all the fields to write to the database , corresponds to the configuration file Insert the contents of the statement .
Using system.web;using system.web.sessionstate;namespace logdb{//<summary>///logmessage Summary description///</ Summary> public class Logmessage:irequiressessionstate {public LogMessage () {} public Logmessag E (String UserID, String username,string ip,string clazz,string method,string result) {This.userid = use RID; This.username = Username; This.ip = IP; This.clazz = Clazz; This.method = method; This.result = result; } private string UserID; public string Userid {get {return Userid;} set {UserID = value;} } private string Username; public string Username {get {return Username;} set {username = value;} } private string IP; public string IP {get{return IP;} Set{ip=value;} } private string Clazz; public string Clazz {Get{return clazz;} Set{clazz=value;} } private String method; public string Method {Get{return method;} Set{method=value;} } private string result; public string Result {get {return Result;} set {result = value;} } }}
a secondCustomLayoutclass.
This class customizes the log output type , The idea is to put the custom fields into the Hashtable , How many custom fields are written ADD Number of , the field name and configuration file remain the same , the namespaces and configuration files here correspond to the following sections .
Using system;using system.collections;using system.io;using log4net. Core;using log4net. Layout.pattern;using log4net. Util;using log4net. Layout;namespace logdb{public class customlayout:log4net. Layout.layoutskeleton {Public const string defaultconversionpattern = "%message%newline"; Public Const string Detailconversionpattern = "%timestamp [%thread]%level%logger%NDC-%message%newline"; private static Hashtable s_globalrulesregistry; private string M_pattern; Private Patternconverter M_head; Private Hashtable m_instancerulesregistry = new Hashtable (); Static CustomLayout () {s_globalrulesregistry = new Hashtable (6); S_globalrulesregistry.add ("username", typeof (Usernamepatternconverter)); S_globalrulesregistry.add ("userid", typeof (Useridpatternconverter)); S_globalrulesregistry.add ("IP", typeof (Ippatternconverter)); S_globalrulesregistry.add ("Clazz", typeof (ClazzpatTernconverter)); S_globalrulesregistry.add ("Method", typeof (Methodpatternconverter)); S_globalrulesregistry.add ("Result", typeof (Resultpatternconverter)); }//--------------------------------------------------------------------public customlayout (): This (Defaultconversionpattern) {} public CustomLayout (string pattern) {Ignoresexception = true; M_pattern = pattern; if (M_pattern = = null) {M_pattern = Defaultconversionpattern; } activateoptions (); } public string Conversionpattern {get {return m_pattern;} set {M_pattern = value;} }///<summary>///Convert values in hashtable///</summary>//<param name= "pattern" & gt;</param>//<returns></returns> virtual protected Patternparser Createpatternparser (St Ring PAttern) {Patternparser patternparser = new Patternparser (pattern); foreach (DictionaryEntry entry in s_globalrulesregistry) {Patternparser.patternconverters[entry . Key] = entry. Value; } foreach (DictionaryEntry entry in m_instancerulesregistry) {PATTERNPARSER.PATTERNC Onverters[entry. Key] = entry. Value; } return patternparser; } override public void Activateoptions () {m_head = Createpatternparser (M_pattern). Parse (); Patternconverter curconverter = M_head; while (curconverter! = null) {Patternlayoutconverter layoutconverter = Curconverter as Patternl Ayoutconverter; if (layoutconverter! = null) {if (!layoutconverter.ignoresexception) {this. Ignoresexception = false; BreaK }} curconverter = Curconverter.next; }} override public void Format (TextWriter writer, loggingevent loggingevent) {if (writer = = null) {throw new ArgumentNullException ("writer"); } if (loggingevent = = null) {throw new ArgumentNullException ("Loggingevent"); } patternconverter C = M_head; while (c! = null) {C.format (writer, loggingevent); c = C.next; }} public void Addconverter (Converterinfo converterinfo) {addconverter (Converterinfo.nam E, Converterinfo.type); } public void Addconverter (string name, type type) {if (name = = null) throw new ARGUMENTNULLEXCE Ption ("name"); if (type = = null) throw new ArgumentNullException ("type"); if (!typeof (PatterNconverter). IsAssignableFrom (type)) {throw new ArgumentException ("The converter type specified [" + Type + "] must be a subclass of Log4net. Util.patternconverter "," type "); } M_instancerulesregistry[name] = type; } public sealed class Converterinfo {private string m_name; Private Type M_type; Public Converterinfo () {} public string Name {get {return m_name;} set {m_name = value;} } public Type type {get {return m_type;} set {M_type = value;} } } }}
and then the Useridpatternconverter ./Usernamepatternconverter/Ippatternconverter A series of classes.How many custom fields have a number of classes,and theCustomLayoutstay consistent below.
Logutilclass.
Logutil class encapsulates the method of logging , Common External calls , parameters define fields for each , Method Body Call Log 4net the method .
A summary description of the Using log4net;///<summary>///logutil//</summary>public class logutil{public logutil () { } Private Logdb.logmessage message = NULL; public void Writelog (string strloggername, String UserID, String username,string ip,string clazz,string method,string Res Ult) { log4net. ILog log = log4net. Logmanager.getlogger (strloggername); message = new Logdb.logmessage (UserID, Username,ip,clazz,method,result);//Used to field auxiliary class log. Info (message);} }
test our project..
Add default . aspx, writing tests in the background code :
namespace logdb{public partial class Default:System.Web.UI.Page { protected void Page_Load (object sender , EventArgs e) { Logutil logutil = new Logutil (); Logutil.writelog ("Authoritysystem", "1", "1", "1", "1", "1", "1"); Logutil.writelog ("Basesystem", "1", "1", "1", "1", "1", "1"); Logutil.writelog ("Examsystem", "1", "1", "1", "1", "1", "1"); Logutil.writelog ("Evaluationsystem", "1", "1", "1", "1", "1", "1"); Logutil.writelog ("Freshsystem", "1", "1", "1", "1", "1", "1");}}}
Run after build , all the operations are recorded in the database. , 5 each subsystem log is recorded in the 5 in the table .
". NET" Log4net writes the operation log to the database