7. Add log capability for presentation layer project 1. Add the following code in Web. Config: configdivsdivnamelog4nettypelog4net. Config. Log4NetConfigurationdivHandler, log4netconfigdivslog4netrootlevelvalueINFOappender-refrefLogFileAppendera
7. Add log capabilities for the presentation layer project 1. add the following code to Config: configps p name = "log4net" type = "log4net. config. log4NetConfigurationpHandler, log4net "// configps log4net root level value =" INFO "/appender-ref =" LogFileAppender "/
7. Add logging capabilities for presentation layer Projects
1. Add the following code in Web. Config:<Configps> <p name = "log4net" type = "log4net. config. log4NetConfigurationpHandler, log4net "/> </configps> <log4net> <root> <level value =" INFO "/> </root> <param name =" File "value =" log-file.txt "/> <param name = "AppendToFile" value = "false"/> <layout type = "log4net. layout. patternLayout "> <param name =" Header "value =" [Header]/r/n "/> <param name =" Footer "value =" [Footer]/r/n "/> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c [% x]-% m % n "/> </layout> <filter type = "log4net. filter. levelRangeFilter "> <param name =" LevelMin "value =" INFO "/> <param name =" LevelMax "value =" WARN "/> </filter> </appender> <layout type = "log4net. layout. patternLayout "> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c [% x]-% m % n "/> </layout> <filter type = "log4net. filter. levelRangeFilter "> <param name =" LevelMin "value =" DEBUG "/> <param name =" LevelMax "value =" WARN "/> </filter> </appender> </log4net>
2. Add the following code to the AssemblyInfo. cs file of Properties in the persistence layer project OfficeDAL:
[Assembly: log4net. Config. XmlConfigurator (ConfigFile = "Web. Config", Watch = true)]
When you run a web project, the log file log-file.txt will be generated under the project.
3. We can add our own log code in the persistence layer for log output:
Using System; using System. collections. generic; using System. text; using nhibmodel; using OffficeModel. pagination; using nhib.pdf. criterion; using System. collections; using log4net; namespace OfficeDAL {public class BaseService {protected ILog log = LogManager. getLogger (typeof (BaseService); protected ISession GetSession () {log. info ("Office: Session"); return HibernateSessionFactory. getSession ();}// /<Summary> /// save /// </summary> /// <param name = "obj"> </param> public void Persist (Object obj) {ISession session = null; ITransaction tx = null; try {session = HibernateSessionFactory. getSession (); tx = session. beginTransaction (); session. persist (obj); tx. commit ();} catch {tx. rollback (); throw;} finally {session. close () ;}/// <summary> // delete an object /// </summary> /// <param name = "obj"> </par Am> public void Remove (Object obj) {ISession session = null; ITransaction tx = null; try {session = HibernateSessionFactory. getSession (); tx = session. beginTransaction (); session. delete (obj); tx. commit ();} catch {tx. rollback (); throw;} finally {session. close ();}} /// <summary> /// query all /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "entiyType"> </param> // <returns> </r Eturns> public IList <T> findAll <T> () where T: new () {log. info ("Office: BaseService FindAll"); ISession session = null; try {session = HibernateSessionFactory. getSession (); return session. createCriteria (typeof (T )). list <T> ();} finally {session. close ();}} /// <summary> /// query all /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "entiyType"> </param> // <returns> </returns> public IList GetAll (Type entityType) {ISession session = null; try {session = HibernateSessionFactory. getSession (); return session. createCriteria (entityType ). list ();} finally {session. close ();}} /// <summary> /// query object oid /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "oid"> </param> // <returns> </returns> public T findById <T> (object oid) where T: new () {ISession session = Null; try {session = HibernateSessionFactory. GetSession (); return session. Get <T> (oid) ;}finally {if (session! = Null) {session. close ();}}} /// <summary> /// modify the object /// </summary> /// <param name = "obj"> </param> public void Update (object obj) {using (ISession session = HibernateSessionFactory. getSession () {session. update (obj); session. close () ;}} public void DoPager (PageInfo pi) {log. info ("Office: DoPager"); if (pi. entityType = null) {throw new Exception ("the paging class name cannot be blank");} using (ISession session = Hibe RnateSessionFactory. getSession () {ICriteria qbc = session. createCriteria (pi. entityType); // The total number of items qbc. setProjection (nhib.pdf. criterion. projections. rowCount (); prepareConditions (qbc, pi. conditions); pi. recordCount = qbc. setMaxResults (1 ). uniqueResult <int> (); // total page number pi. pageCount = pi. recordCount % pi. pageSize = 0? Pi. recordCount/pi. pageSize: pi. recordCount/pi. pageSize + 1; // qbc. setProjection (null); // The paging result ICriteria _ qbc = session. createCriteria (pi. entityType); prepareConditions (_ qbc, pi. conditions); // you can specify prepareOrder (_ qbc, pi. orderFields); // The paging result pi. list = _ qbc. setFirstResult (pi. pageIndex-1) * pi. pageSize ). setMaxResults (pi. pageSize ). list () ;}/// <summary> /// processing condition /// </summary> /// <param n Ame = "qbc"> </param> // <param name = "conditions"> </param> private void prepareConditions (ICriteria qbc, params NCondition [] conditions) {if (qbc = null | conditions. length = 0) {return;} foreach (NCondition condition in conditions) {switch (condition. operate) {case Operation. EQ: qbc. add (Expression. eq (condition. propertyName, condition. propertyValue); break; case Operati On. GT: qbc. add (Expression. gt (condition. propertyName, condition. propertyValue); break; case Operation. LT: qbc. add (Expression. lt (condition. propertyName, condition. propertyValue); break; case Operation. GE: qbc. add (Expression. ge (condition. propertyName, condition. propertyValue); break; case Operation. LE: qbc. add (Expression. le (condition. propertyName, condition. propertyValue); break; case Operation. N E: qbc. add (Expression. not (Expression. eq (condition. propertyName, condition. propertyValue); break; case Operation. BETWEEN: qbc. add (Expression. between (condition. propertyName, (condition. propertyValue as Object []) [0], (condition. propertyValue as Object []) [1]); break; case Operation. LIKE: qbc. add (Expression. like (condition. propertyName, condition. propertyValue. toString (), MatchMode. anywhere )); Break; case Operation. IN: qbc. add (Expression. in (condition. propertyName, condition. propertyValue as object []); break ;}}} /// <summary> /// sort by processing /// </summary> /// <param name = "qbc"> </param> /// <param name = "orderFields"> </param> private void prepareOrder (ICriteria qbc, params OffficeModel. pagination. NOrder [] orderFields) {if (qbc = null | orderFields. length = 0) {r Eturn;} foreach (OffficeModel. Pagination. NOrder order in orderFields) {qbc. AddOrder (order. OrderType = OffficeModel. Pagination. NOrder. OrderDirection. ASC? Order. asc (order. propertyName): Order. desc (order. propertyName ));}} /// <summary> /// query an object based on a single attribute /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "propertyName"> </param> // <param name = "propertyValue"> </param> // <returns> </returns> public T FindByProperty <t> (String propertyName, object propertyValue) {using (ISession session = HibernateSessionFactory. getSession () {return Session. createCriteria (typeof (T )). add (Restrictions. eq (propertyName, propertyValue )). setMaxResults (1 ). uniqueResult <T> () ;}} public T FindByProperty <T> (String [] propertyNames, Object [] propertyValues) {if (propertyNames = null | propertyValues = null | propertyNames. length = 0 | propertyValues. length = 0 | propertyNames. length! = PropertyValues. length) {return default (T);} using (ISession session = HibernateSessionFactory. getSession () {ICriteria qbc = session. createCriteria (typeof (T); for (int I = 0; I <propertyNames. length; I ++) {qbc. add (Restrictions. eq (propertyNames [I], propertyValues [I]);} qbc. setMaxResults (1); return qbc. uniqueResult <T> ();}}}}
Observe the log file output: