将调试信息和错误信息放入两个不同的日志文件(Log4net)

时间:2009-04-28 07:17:17

标签: log4net

我正在使用NHibernate和log4net。这是我的错误日志文件的快照,使用我的软件的发行版:

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)

正如您所看到的,所有infodebugexception都拼凑在一起,因此一旦出现错误,就很难筛选文件并查找信息。< / p>

我想将所有异常信息放入一个文件中,并将其他信息放入另一个文件中。我想排除调试信息是发布模式。怎么做?

3 个答案:

答案 0 :(得分:4)

在下面的代码中我使用两个appender使用两个日志文件(一个用于Debug,另一个用于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>

答案 1 :(得分:3)

在log4net.xml中,使用不同的文件appender记录器进行错误和警告

                              

                              

                     

             

有关详细信息,请参阅here

答案 2 :(得分:0)

您是否只能使用Log Parser以您选择的任何方式过滤/订购数据?

Log4Net Dashboard是一个用于此目的的好工具(它有一个free developer version)。