RollingFlatFileTraceListener timestamp

Topics: Logging Application Block
Sep 17, 2012 at 11:18 PM

Hi,

 

I am using RollingFlatFileTraceListener successfully in my project, except I am seeing some inconsitency with the rolling file name and actual date that the file got created.

I would like to know why sometimes my error.log file name is "error.2012-08-23.1.log"

where as the inside the log file's text has "Timestamp: 8/20/2012 6:08:00 AM", which means the error happened on 8/20/2012, where as the log file name suggests the errors on 8/23/2012...

My application needs to scan through these error logs for some admin activity and it's hard to get the correct date's error logs by the file name.

can someone help me here?

 

Thanks,

Sep 18, 2012 at 1:12 AM

When you first create a RollingFlatFileTraceListener the name is error.log (based on your example).  When the file is rolled the current log will be moved to a new file name with a date.  This date is calculated based on the current date when the file is rolled and is not related to any of the log entries in the file.  Also, a roll is only performed before data is written to the file. So, if you had a rolling flat file configured to roll at midnight and logged a few entries on 8/20/2012 but did not log any further entries to 8/23/2012 then the date on the rolled file will be based on 8/23/2012 even though the data in the file is from 3 days earlier.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Sep 19, 2012 at 4:22 PM

Hi Randy,

Thank you for your answer.

My issue is, I am allowing user to do a search the error log based on the date and I am not sure which log files to do this search on.

I thought it would be a standard practice to have a rolling log file based on the date of the log.  Is there any configuration option for the RollingFlatFileTraceListener to FORCE a roll whenever the date changes??

What would you suggest in this situation? I hate to go on the route of writing custom trace listener.

Thanks,

--Shubhada

Sep 19, 2012 at 11:14 PM

You can use RollInterval.Midnight.  This will roll the file after midnight.  However, note that the roll will only be performed when a message is logged.  So if there are no messages logged between midnight and 9am.  And a log entry is going to be written at 9am then the log will roll at 9am before the log entry is written.  Also, the date in the file name will be the current date even though all log information is for the day before.  If you have frequent and continuous log entries written then that should not be an issue but if you go days without logging anything then the log entries in the file could be days off from the date in the file name.  You could have some sort of scheduled task (or thread) that writes a LogEntry just after midnight or an app recycle along with a startup log message.

You may also want to pay attention to differences between the server DateTime and user's DateTime since they could be different.  Also watch out for UTC.

You could also consider alternate designs such as a database or splitting/aggregating files after they have been written.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

 

 

Sep 21, 2012 at 4:04 PM

Thank you for answering my questions.  Actually writing any log entry other than actual error is not correct for the errorlog file. in our application.

 

So only options I could think of is, writing every log entry with the date/time stamp to the database OR writing custom RollingFlatFileTraceListener.

 

Can you show me some code snippet for the database operation?

 

Thanks,

--Shubhada

Sep 22, 2012 at 6:32 AM

Writing to the database would use the same interface as writing to a flat file.  The difference is in the configuration.  See Logging to a Database for the steps in setting up database logging.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 1, 2014 at 2:17 AM
Randy,
I have similar requirement - the log file must be forced to be generated at midnight. I cannot use alternate design solution (database/aggregating).
I don't want to create scheduled task to keep logging to ensure new log file is created after midnight. So can we enforce log file generation at midnight irrespective of any logging event?

Vishal
Apr 1, 2014 at 2:44 AM
Enterprise Library does not support that behavior. You could create a custom trace listener that in combination with a background/long running thread to roll the file at midnight irrespective of whether any logging is occuring. However, if you are using a web application then I don't think you can guarantee that behavior since you have no way of knowing whether the app pool will be running at midnight.


~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to