Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put Debug Info and Error Info into two different log file (Log4net)

Tags:

log4net

I am using NHibernate and log4net. This is a snapshot of my error log file, using a release version of my software:

INFO 2009-04-28 03:07:06 - processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: bpojob.Generated.BusinessObjects.Job
INFO 2009-04-28 03:07:06 - cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: bpojob.Generated.BusinessObjects.Job.JobItems
DEBUG2009-04-28 03:07:06 - cascading to saveOrUpdate: bpojob.Generated.BusinessObjects.JobItem
DEBUG2009-04-28 03:07:06 - unsaved-value: 0
DEBUG2009-04-28 03:07:06 - transient instance of: bpojob.Generated.BusinessObjects.JobItem
DEBUG2009-04-28 03:07:06 - saving transient instance
DEBUG2009-04-28 03:07:06 - saving [bpojob.Generated.BusinessObjects.JobItem#<null>]
DEBUG2009-04-28 03:07:06 - executing insertions
DEBUG2009-04-28 03:07:06 - executing identity-insert immediately
DEBUG2009-04-28 03:07:06 - Inserting entity: bpojob.Generated.BusinessObjects.JobItem (native id)
DEBUG2009-04-28 03:07:06 - Opened new IDbCommand, open IDbCommands: 1
DEBUG2009-04-28 03:07:06 - Building an IDbCommand object for the SqlString: INSERT INTO job_items (FileName, Job_Id, Status) VALUES (?, ?, ?)
DEBUG2009-04-28 03:07:06 - Dehydrating entity: [bpojob.Generated.BusinessObjects.JobItem#<null>]
DEBUG2009-04-28 03:07:06 - binding 'Blue hills.jpg' to parameter: 0
DEBUG2009-04-28 03:07:06 - binding '8' to parameter: 1
DEBUG2009-04-28 03:07:06 - binding '1' to parameter: 2
DEBUG2009-04-28 03:07:06 - INSERT INTO job_items (FileName, Job_Id, Status) VALUES (?p0, ?p1, ?p2); ?p0 = 'Blue hills.jpg', ?p1 = '8', ?p2 = '1'
DEBUG2009-04-28 03:07:06 - Obtaining IDbConnection from Driver
DEBUG2009-04-28 03:07:06 - Closed IDbCommand, open IDbCommands: 0
DEBUG2009-04-28 03:07:06 - aggressively releasing database connection
DEBUG2009-04-28 03:07:06 - Closing connection
DEBUG2009-04-28 03:07:06 - could not insert: [bpojob.Generated.BusinessObjects.JobItem]
[ INSERT INTO job_items (FileName, Job_Id, Status) VALUES (?p0, ?p1, ?p2) ]

MySql.Data.MySqlClient.MySqlException: Duplicate entry 'Blue hills.jpg' for key 'Unique'
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadResult(UInt64& affectedRows, Int64& lastInsertId)
   at MySql.Data.MySqlClient.MySqlDataReader.GetResultSet()
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
   at NHibernate.Id.Insert.AbstractSelectingDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)

As you can see, all the info, debug and exception are cobbled together, making it extremely hard to sift through the file and look for information once bugs come out.

I want to put all the exception info into a file, and put other info into another file. And I want to exclude the debug information is release mode. How to do this?

like image 547
Graviton Avatar asked Feb 28 '23 19:02

Graviton


2 Answers

In the below code i am using two log files using two appender(one for Debug,another for Fatal)

<log4net>
<appender name="FatalErrorLog" type="log4net.Appender.RollingFileAppender">
  <file value="C:\temp\Host.log" />
  <appendToFile value="true" />
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="50" />
  <rollingStyle value="Size" />
  <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
     <levelMin value="ERROR" />
     <levelMax value="FATAL" />
  </filter>
</appender>

<appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
<file value="C:\temp\HostDebug.log" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="50" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%identity------%username------%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="DEBUG" />
  <levelMax value="ERROR" />
</filter>
</appender>
</log4net>
like image 137
Ash Avatar answered Apr 27 '23 12:04

Ash


In your log4net.xml use a different file appender logger for Error and Warning

See here for more info

like image 34
Preet Sangha Avatar answered Apr 27 '23 12:04

Preet Sangha